【发布时间】:2015-03-19 07:29:11
【问题描述】:
//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 + " ");
}
}
为什么这个方法 public synchronized Safe (String S) 给我一个编译错误?我知道我们不能同步变量但是上面的代码有什么问题?!?!
【问题讨论】:
-
你希望它做什么?在构建之前,您不能共享对象。
标签: java