Question 1261. import java.util.*;
2.
3. public class NameList {
4. private List names = new ArrayList();
5. public synchronized void add(String name) { names.add(name); }
6. public synchronized void printAll() {
7. for (int i = 0; i <names.size(); i++) {
8. System.out.print(names.get(i) +“ “);
9. }
10. }
11. public static void main(String[] args) {
12. final NameList sl = new NameList();
13 .for(int i=0;i<2;i++) {
14. new Thread() {
15. public void run() {
16. sl.add(”A”);
17. sl.add(”B”);
18. sl.add(”C”);
19. sl.printAll();
20. }
21. }.start();
22. }
23. }
24. }
Which two statements are true if this class is compiled and run?
(Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output “A B A B C C “, then exit.
E. The code may rum with output “A B C A B C A B C “, then exit.
F. The code may ruin with output “A A A B C A B C C “, then exit.
G. The code may ruin with output “A B C A A B C A B C “, then exit.
答案是EG
E我是理解的,只是G……哪位大侠能帮忙解释一下?感激不尽……
2.
3. public class NameList {
4. private List names = new ArrayList();
5. public synchronized void add(String name) { names.add(name); }
6. public synchronized void printAll() {
7. for (int i = 0; i <names.size(); i++) {
8. System.out.print(names.get(i) +“ “);
9. }
10. }
11. public static void main(String[] args) {
12. final NameList sl = new NameList();
13 .for(int i=0;i<2;i++) {
14. new Thread() {
15. public void run() {
16. sl.add(”A”);
17. sl.add(”B”);
18. sl.add(”C”);
19. sl.printAll();
20. }
21. }.start();
22. }
23. }
24. }
Which two statements are true if this class is compiled and run?
(Choose two.)
A. An exception may be thrown at runtime.
B. The code may run with no output, without exiting.
C. The code may run with no output, exiting normally.
D. The code may rum with output “A B A B C C “, then exit.
E. The code may rum with output “A B C A B C A B C “, then exit.
F. The code may ruin with output “A A A B C A B C C “, then exit.
G. The code may ruin with output “A B C A A B C A B C “, then exit.
答案是EG
E我是理解的,只是G……哪位大侠能帮忙解释一下?感激不尽……
解决方案 »
- java 与c 汉字 ascii问题
- 请大侠帮忙修改一下JAVA程序
- 遇到的JAVA问题 大家来看看 顺便祝大家5 1节快乐
- 刚开始学java,问一个很基本的问题
- JMF的问题!我不信没人知道!!!
- 对于MVC模式,深度讨论其View层细化问题,使用组件开发模式,来简化用户需求!(大家得工作可以不用那么累了,都来看看!)
- mysql 1044错误!!!
- JavaExcelApi 和 POI-HSSF 读取计算字段的值好像都有问题,怎么办?
- 请问,如何监听类似VB中TextBox控件的Text_Change()事件
- 不会就问
- 正则 String a="" 匹配啥?
- 请问用JAVA怎么访问百度?谢谢
A A B B C A A B B C C
我也是初学者,这是在我的机子上测试得出的结果之一,我认为不会得出答案F. The code may ruin with output “A A A B C A B C C “, then exit. 当然答案D不在考虑范围。这个题目是不是要用排除法呢
import java.util.List;public class Main { 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) + " ");
}
}
static int j = 0;
public static void main(String[] args) {
final Main sl = new Main();
for (int i = 0; i < 2; i++) {
if (i == 0) {
j = 0;
} else {
j = 1;
}
Thread t = new Thread() {
public void run() {
sl.add("A" + j);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
sl.add("B" + j);
sl.add("C" + j);
sl.printAll();
System.out.println("===");
}
};
t.start();
try {
Thread.sleep(500);
} catch (InterruptedException e) {
}
}
}
}
--------
他的这个答案是错误的,不知道你是如何理解的。