itgle.com

classThreadBothextendsThreadimplementsRunnable{publicvoidrun(){System.out.print("hi&qclassThreadBothextendsThreadimplementsRunnable{publicvoidrun(){System.out.print("hi");}publicstaticvoidmain(String[]args){Threadt1=newThreadBoth();Threadt2=newThread(t1);

题目
classThreadBothextendsThreadimplementsRunnable{publicvoidrun(){System.out.print("hi&q

classThreadBothextendsThreadimplementsRunnable{publicvoidrun(){System.out.print("hi");}publicstaticvoidmain(String[]args){Threadt1=newThreadBoth();Threadt2=newThread(t1);t1.run();t2.run();}}结果为:()

A.hi

B.hihi

C.编译失败

D.代码运行,但无输出结果


相似考题
更多“classThreadBothextendsThreadimplementsRunnable{publicvoidrun(){System.out.print("hi&q ”相关问题
  • 第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?()


    参考答案:F

  • 第2题:

    Runnabler=newRunnable(){publicvoidrun(){System.out.print(”Cat”);}};Threadt=newThread(r){publicvoidrun(){System.out.print(”Dog”);}};t.start();Whatistheresult?()

    A.Cat

    B.Dog

    C.Compilationfails.

    D.Thecoderunswithnooutput.

    E.Anexceptionisthrownatruntime.


    参考答案:B

  • 第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?()


    参考答案:B, D

  • 第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”.


    参考答案:E

  • 第5题:

    publicclassThreads5{publicstaticvoidmain(String[]args){newThread(newRunnable(){publicvoidrun(){System.out.print(”bar”);}}).start();}}Whatistheresult?()

    A.Compilationfails.

    B.Anexceptionisthrownatruntime.

    C.Thecodeexecutesnormallyandprints“bar”.

    D.Thecodeexecutesnormally,butnothingprints.


    参考答案:C