执行的结果明明没有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();
}
}
}
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();
}
}
}
有这个方法吗?我怎么没见过?应该是run()吧
A B C A B C A B C
你想问什么呢?没明白你的意思
你应该再建一个public void run(){}方法才行的
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 题时得注意,其中有很多文字上的错误,理解原题意思和考点就行。