//What will happen when you attempt to compile and run the following code?
public class TestThread extends Thread {
public static void main(String[] args) {
new TestThread().start();
new TestThread().start();
}
public void run() {
Safe s1 = new Safe("abc");
Safe s2 = new Safe("xyz");
}
}
class Safe {
String str;
public synchronized Safe(String s) {
str = s;
str = str.toUpperCase();
System.out.print(str + " ");
}
}
为什么这个方法公开同步安全(字符串S)给我一个编译错误?我知道我们不能同步变量,但是上面的代码有什么问题?!?!为什么这种同步方法给我一个错误?
你期望它做什么?在构建完成之前,您无法共享对象。 – 2015-01-20 22:24:40