可学答题网 > 问答 > SCJP程序员认证考试题库
目录: 标题| 题干| 答案| 搜索| 相关
问题

public class TestFive 


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 }.
参考答案
参考解析:
分类:SCJP程序员认证考试题库
相关推荐

1、10. public class Class

10.&ensp;public&ensp;class&ensp;ClassA&ensp;{&ensp;11.&ensp;public&ensp;void&ensp;methodA()&ensp;{&ensp;12.&ensp;ClassB&ensp;classB&ensp;=&ensp;new&ensp;ClassB()...

2、public class X { 

public&ensp;class&ensp;X&ensp;{&ensp;public&ensp;static&ensp;void&ensp;main(String&ensp;[]&ensp;args)&ensp;{&ensp;try&ensp;{&ensp;badMethod();&ensp;System.out.p...

3、10. public class Class

10.&ensp;public&ensp;class&ensp;ClassA&ensp;{&ensp;11.&ensp;public&ensp;void&ensp;count(int&ensp;i)&ensp;{&ensp;12.&ensp;count(++i);&ensp;13.&ensp;}&ensp;14.&en...

4、public class X { 

public&ensp;class&ensp;X&ensp;{&ensp;public&ensp;static&ensp;void&ensp;main&ensp;(String[]&ensp;args)&ensp;&ensp;{&ensp;byte&ensp;b&ensp;=&ensp;127;&ensp;byte&ens...

5、 public class X {

&ensp;public&ensp;class&ensp;X&ensp;{&ensp;&ensp;public&ensp;static&ensp;void&ensp;main&ensp;(String[]args)&ensp;&ensp;&ensp;{&ensp;&ensp;string&ensp;s&ensp;=&ensp...

6、public class X { 

public&ensp;class&ensp;X&ensp;{&ensp;public&ensp;static&ensp;void&ensp;main&ensp;(String[]args)&ensp;&ensp;{&ensp;String&ensp;s1&ensp;=&ensp;new&ensp;String&ensp;...