现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print("hi");}publicstaticvoiclmain(String[]args){Threadtl=newThreadBoth():Threadt2=newThread(tl):tl.run():t2.run():}结果为:()
A.hihi
B.hi
C.编译失败
D.运行时异常被抛出
第1题:
publicclassTwoThreads{
privatestaticObjectresource=newObject();
privatestaticvoiddelay(longn){
try{Thread.sleep(n);}
catch(Exceptione){System.out.print(”Error);}
}
publicstaticvoidmain(String[]args){
System.out.print(”StartMain);
newThread1().start();
delay(1000);
Threadt2=newThread2();
t2.start();
delay(1000);
t2.interrupt
delay(1000);
System.out.print(”EndMain);
}
staticclassThread1extendsThread{
publicvoidrun(){
synchronized(resource){
System.out.print(”Startl);
delay(6000);
System.out.print(”End1);
}
}
}
staticclassThread2extendsThread{
publicvoidrun(){
synchronized(resource){
System.out.print(”Start2);
delay(2000);
System.out.print(”End2);
}
}
}
}
Assumethatsleep(n)executesinexactlymmilliseconds,andallothercodeexecutesinaninsignificantamountoftime.Whatistheoutputifthemain()methodisrun?()
第2题:
A.Cat
B.Dog
C.Compilationfails.
D.Thecoderunswithnooutput.
E.Anexceptionisthrownatruntime.
第3题:
publicclassSyncTest{
publicstaticvoidmain(Stringargs){
finalStringBuffers1=newStringBuffer();
finalStringBuffers2=newStringBuffer();
newThread(){
publicvoidrun(){
synchronized(s1){
s2.append(A”);
synchronized(s2){
s2.append(B”);
System.out.print(s1);
System.out.print(s2);
}
}
}
}.start();
newThread(){
publicvoidrun(){
synchronized(s2){
s2.append(C”);
synchronized(s1){
s1.append(D”);
System.out.print(s2);
System.out.print(s1);
}
}
}
}.start();
}
}
Whichtwostatementsaretrue?()
第4题:
publicclassTestOneimplementsRunnable{publicstaticvoidmain(String[]args)throwsException{Threadt=newThread(newTestOne());t.start();System.out.print(”Started”);t.join();System.out.print(”Complete”);}publicvoidrun(){for(inti=0;i<4;i++){System.out.print(i);}}}Whatcanbearesult?()
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesandprints“StartedComplete”.
D.Thecodeexecutesandprints“StartedComplete0123”.
E.Thecodeexecutesandprints“Started0l23Complete”.
第5题:
A.Compilationfails.
B.Anexceptionisthrownatruntime.
C.Thecodeexecutesnormallyandprints“bar”.
D.Thecodeexecutesnormally,butnothingprints.