2009年2月26日 星期四

第224題

Given:
1. public class Threads4{
2. public static void main(String[] args){
3. new Threads4.go();
4. }
5. public void go(){
6. Runnable r = new Runnable(){
7. public void run(){
8. System.out.print("foo");
9. }
10. };
11. Thread t = new Thread(r);
12. t.start();
13. t.start();
14. }
15. }

What is the result?

A. Compilation fails.
B. An exception is thrown at runtime.
C. The code executes normally and prints "foo";
D. The code executes normally, but nothing is printed.
答案:B
參考:13-4 執行緒的同步性與安全性
------------------------------------------------
13. t.start(); //ERROR Thread 己經在12行產生了,
這行會產生exception IllegalThreadStateException

沒有留言:

張貼留言