A.Cat
B.Dog
C.Compilationfails.
D.Thecoderunswithnooutput.
E.Anexceptionisthrownatruntime.
第1题:
publicclassThreads4{publicstaticvoidmain(String[]args){newThreads4().go();}publicvoidgo(){Runnabler=newRunnable(){publicvoidrun(){System.out.print(”foo”);}};Threadt=newThread(r);t.start();t.start();}}Whatistheresult?()
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesnormallyandprints?foo”.
D.Thecodeexecutesnormally,butnothingisprinted.
第2题:
A.newThread(){publicvoidrun(){doStuff();}}
B.newThread(){publicvoidstart(){doStuff();}}
C.newThread(){publicvoidstart(){doStuff();}}.run();
D.newThread(){publicvoidrun(){doStuff();}}.start();
E.newThread(newRunnable(){publicvoidrun(){doStuff();}}).run();
F.newThread(newRunnable(){publicvoidrun(){doStuff();}}).start();
第3题:
classMyThreadextendsThread{publicvoidrun(){System.out.println(AAA”);}publicvoidrun(Runnabler){System.out.println(BBB”);}publicstaticvoidmain(String[]args){newThread(newMyThread()).start();}}Whatistheresult?()
A.AAA
B.BBB
C.Compilationfails.
D.Thecoderunswithnooutput.
第4题:
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesandprints“running”.
D.Thecodeexecutesandprints“runningrunning”.
E.Thecodeexecutesandprints“runningrunningrunning”.
第5题:
GiventhatastaticmethoddoIt()inaclassWorkrepresentsworktobedone,whatblockofcodewillsucceedinstartinganewthreadthatwilldothework?
CODEBLOCKa:
Runnabler=newRunnable(){
publicvoidrun(){
Work.doIt();
}
};
Threadt=newThread(r);
t.start();
CODEBLOCKb:
Threadt=newThread(){
publicvoidstart(){
Work.doIt();}};
t.start();
CODEBLOCKc:
Runnabler=newRunnable(){
publicvoidrun(){
Work.doIt();
}
};
r.start();
CODEBLOCKd:
Threadt=newThread(newWork());
t.start();
CODEBLOCKe:
Runnablet=newRunnable(){
publicvoidrun(){
Work.doIt();
}
};
t.run();