Java StringBuffer在线程安全程序的设计中是如何工作的?
许多人提到了Java中StringBuffer和StringBuilder之间的区别。StringBuffer包含同步方法。人们会说“如果一个缓冲区被许多线程使用,那么就使用StringBuffer。”但是使用StringBuffer真的能保证“线程安全”吗?好吧,我认为强调StringBuffer的一些实际用途很重要。为此,我设计了一个简单的程序来说明StringBuffer在实现线程安全方面如何优于StringBuilderJava StringBuffer在线程安全程序的设计中是如何工作的?,java,multithreading,concurrency,stringbuilder,stringbuffer,Java,Multithreading,Concurrency,Stringbuilder,Stringbuffer,许多人提到了Java中StringBuffer和StringBuilder之间的区别。StringBuffer包含同步方法。人们会说“如果一个缓冲区被许多线程使用,那么就使用StringBuffer。”但是使用StringBuffer真的能保证“线程安全”吗?好吧,我认为强调StringBuffer的一些实际用途很重要。为此,我设计了一个简单的程序来说明StringBuffer在实现线程安全方面如何优于StringBuilder /** * Run this program a couple
/**
* Run this program a couple of times. We see that the StringBuilder does not
* give us reliable results because its methods are not thread-safe as compared
* to StringBuffer.
*
* For example, the single append in StringBuffer is thread-safe, i.e.
* only one thread can call append() at any time and would finish writing
* back to memory one at a time. In contrast, the append() in the StringBuilder
* class can be called concurrently by many threads, so the final size of the
* StringBuilder is sometimes less than expected.
*
*/
public class StringBufferVSStringBuilder {
public static void main(String[] args) throws InterruptedException {
int n = 10;
//*************************String Builder Test*******************************//
StringBuilder sb = new StringBuilder();
StringBuilderTest[] builderThreads = new StringBuilderTest[n];
for (int i = 0; i < n; i++) {
builderThreads[i] = new StringBuilderTest(sb);
}
for (int i = 0; i < n; i++) {
builderThreads[i].start();
}
for (int i = 0; i < n; i++) {
builderThreads[i].join();
}
System.out.println("StringBuilderTest: Expected result is 1000; got " + sb.length());
//*************************String Buffer Test*******************************//
StringBuffer sb2 = new StringBuffer();
StringBufferTest[] bufferThreads = new StringBufferTest[n];
for (int i = 0; i < n; i++) {
bufferThreads[i] = new StringBufferTest(sb2);
}
for (int i = 0; i < n; i++) {
bufferThreads[i].start();
}
for (int i = 0; i < n; i++) {
bufferThreads[i].join();
}
System.out.println("StringBufferTest: Expected result is 1000; got " + sb2.length());
}
}
// Every run would attempt to append 100 "A"s to the StringBuilder.
class StringBuilderTest extends Thread {
StringBuilder sb;
public StringBuilderTest (StringBuilder sb) {
this.sb = sb;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb.append("A");
}
}
}
//Every run would attempt to append 100 "A"s to the StringBuffer.
class StringBufferTest extends Thread {
StringBuffer sb2;
public StringBufferTest (StringBuffer sb2) {
this.sb2 = sb2;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb2.append("A");
}
}
}
当然,它是“线程安全的”,即不存在数据竞争(基本上是没有足够同步的并发访问)。但是您不知道线程3将打印什么:“ab”或“ba”“。我必须引入更多的同步,使其产生合理的结果。StringBuffer附带的锁没有任何帮助。”
我希望这对您有深刻的见解!好吧,我认为强调StringBuffer的一些实际用途是很重要的。为此,我设计了一个简单的程序来说明StringBuffer在实现线程安全方面如何优于StringBuilder
/**
* Run this program a couple of times. We see that the StringBuilder does not
* give us reliable results because its methods are not thread-safe as compared
* to StringBuffer.
*
* For example, the single append in StringBuffer is thread-safe, i.e.
* only one thread can call append() at any time and would finish writing
* back to memory one at a time. In contrast, the append() in the StringBuilder
* class can be called concurrently by many threads, so the final size of the
* StringBuilder is sometimes less than expected.
*
*/
public class StringBufferVSStringBuilder {
public static void main(String[] args) throws InterruptedException {
int n = 10;
//*************************String Builder Test*******************************//
StringBuilder sb = new StringBuilder();
StringBuilderTest[] builderThreads = new StringBuilderTest[n];
for (int i = 0; i < n; i++) {
builderThreads[i] = new StringBuilderTest(sb);
}
for (int i = 0; i < n; i++) {
builderThreads[i].start();
}
for (int i = 0; i < n; i++) {
builderThreads[i].join();
}
System.out.println("StringBuilderTest: Expected result is 1000; got " + sb.length());
//*************************String Buffer Test*******************************//
StringBuffer sb2 = new StringBuffer();
StringBufferTest[] bufferThreads = new StringBufferTest[n];
for (int i = 0; i < n; i++) {
bufferThreads[i] = new StringBufferTest(sb2);
}
for (int i = 0; i < n; i++) {
bufferThreads[i].start();
}
for (int i = 0; i < n; i++) {
bufferThreads[i].join();
}
System.out.println("StringBufferTest: Expected result is 1000; got " + sb2.length());
}
}
// Every run would attempt to append 100 "A"s to the StringBuilder.
class StringBuilderTest extends Thread {
StringBuilder sb;
public StringBuilderTest (StringBuilder sb) {
this.sb = sb;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb.append("A");
}
}
}
//Every run would attempt to append 100 "A"s to the StringBuffer.
class StringBufferTest extends Thread {
StringBuffer sb2;
public StringBufferTest (StringBuffer sb2) {
this.sb2 = sb2;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb2.append("A");
}
}
}
当然,它是“线程安全的”,即没有数据竞争(基本上是没有足够同步的并发访问)。但您不知道线程3将打印什么:“ab”或“ba”。我必须引入更多的同步,使其产生合理的结果。StringBuffer附带的锁没有任何帮助。”
我希望这对您有深刻的见解!好吧,我认为强调StringBuffer的一些实际用途是很重要的。为此,我设计了一个简单的程序来说明StringBuffer在实现线程安全方面如何优于StringBuilder
/**
* Run this program a couple of times. We see that the StringBuilder does not
* give us reliable results because its methods are not thread-safe as compared
* to StringBuffer.
*
* For example, the single append in StringBuffer is thread-safe, i.e.
* only one thread can call append() at any time and would finish writing
* back to memory one at a time. In contrast, the append() in the StringBuilder
* class can be called concurrently by many threads, so the final size of the
* StringBuilder is sometimes less than expected.
*
*/
public class StringBufferVSStringBuilder {
public static void main(String[] args) throws InterruptedException {
int n = 10;
//*************************String Builder Test*******************************//
StringBuilder sb = new StringBuilder();
StringBuilderTest[] builderThreads = new StringBuilderTest[n];
for (int i = 0; i < n; i++) {
builderThreads[i] = new StringBuilderTest(sb);
}
for (int i = 0; i < n; i++) {
builderThreads[i].start();
}
for (int i = 0; i < n; i++) {
builderThreads[i].join();
}
System.out.println("StringBuilderTest: Expected result is 1000; got " + sb.length());
//*************************String Buffer Test*******************************//
StringBuffer sb2 = new StringBuffer();
StringBufferTest[] bufferThreads = new StringBufferTest[n];
for (int i = 0; i < n; i++) {
bufferThreads[i] = new StringBufferTest(sb2);
}
for (int i = 0; i < n; i++) {
bufferThreads[i].start();
}
for (int i = 0; i < n; i++) {
bufferThreads[i].join();
}
System.out.println("StringBufferTest: Expected result is 1000; got " + sb2.length());
}
}
// Every run would attempt to append 100 "A"s to the StringBuilder.
class StringBuilderTest extends Thread {
StringBuilder sb;
public StringBuilderTest (StringBuilder sb) {
this.sb = sb;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb.append("A");
}
}
}
//Every run would attempt to append 100 "A"s to the StringBuffer.
class StringBufferTest extends Thread {
StringBuffer sb2;
public StringBufferTest (StringBuffer sb2) {
this.sb2 = sb2;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb2.append("A");
}
}
}
当然,它是“线程安全的”,即没有数据竞争(基本上是没有足够同步的并发访问)。但您不知道线程3将打印什么:“ab”或“ba”。我必须引入更多的同步,使其产生合理的结果。StringBuffer附带的锁没有任何帮助。”
我希望这对您有深刻的见解!好吧,我认为强调StringBuffer的一些实际用途是很重要的。为此,我设计了一个简单的程序来说明StringBuffer在实现线程安全方面如何优于StringBuilder
/**
* Run this program a couple of times. We see that the StringBuilder does not
* give us reliable results because its methods are not thread-safe as compared
* to StringBuffer.
*
* For example, the single append in StringBuffer is thread-safe, i.e.
* only one thread can call append() at any time and would finish writing
* back to memory one at a time. In contrast, the append() in the StringBuilder
* class can be called concurrently by many threads, so the final size of the
* StringBuilder is sometimes less than expected.
*
*/
public class StringBufferVSStringBuilder {
public static void main(String[] args) throws InterruptedException {
int n = 10;
//*************************String Builder Test*******************************//
StringBuilder sb = new StringBuilder();
StringBuilderTest[] builderThreads = new StringBuilderTest[n];
for (int i = 0; i < n; i++) {
builderThreads[i] = new StringBuilderTest(sb);
}
for (int i = 0; i < n; i++) {
builderThreads[i].start();
}
for (int i = 0; i < n; i++) {
builderThreads[i].join();
}
System.out.println("StringBuilderTest: Expected result is 1000; got " + sb.length());
//*************************String Buffer Test*******************************//
StringBuffer sb2 = new StringBuffer();
StringBufferTest[] bufferThreads = new StringBufferTest[n];
for (int i = 0; i < n; i++) {
bufferThreads[i] = new StringBufferTest(sb2);
}
for (int i = 0; i < n; i++) {
bufferThreads[i].start();
}
for (int i = 0; i < n; i++) {
bufferThreads[i].join();
}
System.out.println("StringBufferTest: Expected result is 1000; got " + sb2.length());
}
}
// Every run would attempt to append 100 "A"s to the StringBuilder.
class StringBuilderTest extends Thread {
StringBuilder sb;
public StringBuilderTest (StringBuilder sb) {
this.sb = sb;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb.append("A");
}
}
}
//Every run would attempt to append 100 "A"s to the StringBuffer.
class StringBufferTest extends Thread {
StringBuffer sb2;
public StringBufferTest (StringBuffer sb2) {
this.sb2 = sb2;
}
@Override
public void run() {
for (int i = 0; i < 100; i++) {
sb2.append("A");
}
}
}
当然,它是“线程安全的”,即没有数据竞争(基本上是没有足够同步的并发访问)。但您不知道线程3将打印什么:“ab”或“ba”。我必须引入更多的同步,使其产生合理的结果。StringBuffer附带的锁没有任何帮助。”
我希望这对你有深刻的见解!提问和回答。这是怎么回事???@Youngistan是的,这是允许的。@Youngistan您好..stackoverflow给了我一个分享知识和问答风格的选择。Ohkk我不知道这一点。我已经将此标记为版主注意。问这个问题只是为了在博客上展示答案。它更适合到一篇博客文章。它还复制了其他线程。这个答案可以发布到其他线程中,不会丢失任何东西。询问和回答。这是什么??@Youngistan是的,这是允许的。@Youngistan Hello..stackoverflow给了我一个分享知识、问答风格的选项。Ohkk我没有意识到这一点。我已将此标记为版主att注意。问这个问题只是为了在博客上展示答案。它更适合博客帖子。它还复制了其他线程。这个答案可以发布到其他线程中,不会丢失任何内容。问和答。这是什么??@Youngistan是的,这是允许的。@Youngistan Hello..stackoverflow给了我一个选择sha的选项re knowledge,Q&A风格。Ohkk我没有意识到这一点。我已经将此标记为版主注意。问这个问题只是为了在博客上展示答案。它更适合于博客帖子。它还复制了其他帖子。这个答案可以发布到其他帖子中,不会丢失任何内容。提问和回答。这是什么@Youngistan是的,这是允许的。@Youngistan Hello..stackoverflow为我提供了一个分享知识和问答风格的选项。哦,我不知道这一点。我已将此标记为版主注意。问这个问题只是为了在博客上展示答案。它更适合博客文章。它还复制了其他线程。这个答案可能是博文将其绑定到其他线程之一,不会丢失任何内容。