试题详情
多项选择题 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
  • 关注下方微信公众号,在线模考后查看

热门试题