代码段:
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.
大家认为哪个是正确的答案,欢迎发表意见?(单选)

解决方案 »

  1.   

    E
    public static synchronized
      

  2.   

    The E selection is true.
      

  3.   

    x是static,所以能由N多个Test实例共享并改变,而每个实例的doThings虽然加锁了,却只是加在自身之上,
    其他实例照样畅通无阻。所以现在这程序是不安全的~改法
    1 public static synchronized doThings()

    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();
        }
      }}
    个人拙见,不对的楼下请指正