itgle.com

现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print(&quot现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print("hi");}publicstaticvoiclmain(String[]args){Threadtl=newThreadBoth():Threadt2=newThre

题目
现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print("

现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print("hi");}publicstaticvoiclmain(String[]args){Threadtl=newThreadBoth():Threadt2=newThread(tl):tl.run():t2.run():}结果为:()

A.hihi

B.hi

C.编译失败

D.运行时异常被抛出


相似考题
更多“现有:classThreadBothextendsThreaaimplementsRunnable{publicvoidrun()(System.out.print(&quot ”相关问题
  • 第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