21 public String toString(){
22 StingBuffer buffer = new StringBuffer();
23 buffer.append('<');
24 buffer.append(this.name);
25 buffer.append('>');
26 return buffer.toString();
}
Which statement is true?
A. This code is NOT thread-safe.
B. The programmer can replace StringBuffer with StringBuilder with no other changes.
C. This code will perform poorly. For better performance, the code should be rewritten:
return "<" + this.name + ">";
D. This code will perform well and converting the code to use StringBuilder will not
enhance the performance.
Answer: B
==================
如果两个线程调用这个方法,会有两个StringBuffer,StringBuffer的append()是同步的,但是这个时候他同步在两个不同的StingBuffer对象上,这样的话两个线程能够同时访问name了,这样不就不是线程安全了么?
还有谁能给下线程安全的定义?
这里不是共享了 this.name 这个变量么?
假设一个线程在调用toString() 而另外一个线程在操作这个 name 变量
是不是会出现线程不安全的情况呢?