代码段:
public class Test extends Thread{private static int x;public synchronized void doThings(){
int current =x;
current++;
x=current;
}public void run(){
doThings();
}}Which is true?A. Compilation fails
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized ( new Object() ){} block would make the class thread-safe.
大家认为哪个是正确的答案,欢迎发表意见?(单选)
public class Test extends Thread{private static int x;public synchronized void doThings(){
int current =x;
current++;
x=current;
}public void run(){
doThings();
}}Which is true?A. Compilation fails
B. An exception is thrown at runtime.
C. Synchronizing the run() method would make the class thread-safe.
D. The data in variable "x" are protected from concurrent access problems.
E. Declaring the doThings() method as static would make the class thread-safe.
F. Wrapping the statements within doThings() in a synchronized ( new Object() ){} block would make the class thread-safe.
大家认为哪个是正确的答案,欢迎发表意见?(单选)
public static synchronized
其他实例照样畅通无阻。所以现在这程序是不安全的~改法
1 public static synchronized doThings()
2
public class Test extends Thread{private static int x;
private static Object o=new Object();public synchronized void doThings(){
int current =x;
current++;
x=current;
}public void run(){
synchronized(o){
doThings();
}
}}
个人拙见,不对的楼下请指正