public class TestFive { private int x; public void foo() { int current = x; x = current + 1; } public void go() { for(int i=0;i<5;i++) { new Thread() { public void run() { foo(); System.out.print(x + “, “); } }.start(); }}} Which two changes, taken together, would guarantee the output: 1, 2, 3, 4, 5, ?()
- A、 Move the line 12 print statement into the foo() method.
- B、 Change line 7 to public synchronized void go() {.
- C、 Change the variable declaration on line 3 to private volatile int x;.
- D、 Wrap the code inside the foo() method with a synchronized( this ) block.
- E、 Wrap the for loop code inside the go() method with a synchronized block synchronized(this) { // for loop code here }.
正确答案:A,D
答案解析:有

微信扫一扫手机做题