执行的结果明明没有output为什么答案说是
The code may rum with output “A B C A B C A B C “, then exit.
OR:
The code may ruin with output “A B C A A B C A B C “, then exit.
Given:import java.util.*;public class Question126 {
private List names = new ArrayList();

public synchronized void add(String name) {
names.add(name);
}

public synchronized void printAll() {
for (int i = 0; i <names.size(); i++) {
System.out.print(names.get(i) +" ");
}
}

public static void main(String[] args) {
final Question126 sl = new Question126();
for(int i=0;i<2;i++) {
new Thread() {
public void ruin() {
sl.add("A");
sl.add("B");
sl.add("C");
sl.printAll();
}
}.start();
}
}
}

解决方案 »

  1.   

    public void ruin() 
    有这个方法吗?我怎么没见过?应该是run()吧
      

  2.   

    我运行了三遍,都是
    A B C A B C A B C
    你想问什么呢?没明白你的意思
      

  3.   

    你的这个是多线程,必须要有sart和run的,
    你应该再建一个public void run(){}方法才行的
      

  4.   

    原题应该是
    The code may run with output “A B C A B C A B C “, then exit.
    意识是
    这段代码运行时会输出 A B C A B C A B C 然后退出。
    看 SCJP 题时得注意,其中有很多文字上的错误,理解原题意思和考点就行。