Runnable r = new Runnable() {  public void run() {  System.out.print(”Cat”);  }  };  Threadt=new Thread(r) {  public void run() {  System.out.print(”Dog”);  }  };  t.start();  What is the result?() A、 CatB、 DogC、 Compilation fails.D、 The code runs with n

题目

Runnable r = new Runnable() {  public void run() {  System.out.print(”Cat”);  }  };  Threadt=new Thread(r) {  public void run() {  System.out.print(”Dog”);  }  };  t.start();  What is the result?() 

  • A、 Cat
  • B、 Dog
  • C、 Compilation fails.
  • D、 The code runs with no output.
  • E、 An exception is thrown at runtime.

相似考题
更多“Runnable r = new”相关问题
  • 第1题:

    请阅读下面程序 public class ThreadTest{ public static void main(String args[]) ( Thread t1=new Thread(new Hello()); Thread t2=new Thread(new Hello()); t1.start(); t2.start(); } } class Hello implements Runnable { int i; public void run() { while(true) { System.out.prinfin("Hello"+i++); if(i=5) break; } } } 该程序创建线程使用的方法是

    A.继承Thread类

    B.实现Runnable接口

    C.t1.start()

    D.t2.start()


    正确答案:B
    解析:本题考查线程的创建。Java中,线程的创建有两种方法:
      (1)通过实现Runnable接口创建线程。Runnable接口中只定义了一个run()方法作为线程体。
      (2)通过继承Thread类创建线程。Thread类本身实现了Runnable接口。
      无论使用哪种方法来创建线程,新建的线程不会自动运行,必须调用线程的start()方法,才能运行该线程。
      本题程序中的Hello类实现了Runnable接口,即采用的是第一种方法创建线程。因此,本题的正确答案是选项B。

  • 第2题:

    通过实现Runnable接口创建线程,请在下面横线处填入代码完成此程序。

    注意:不改动程序结构,不得增行或删行。

    class ThreadTest implements Runnable

    {

    Thread thrObj;

    public static void main(String args[])

    {

    System.out.println("这是一个通过实现接口创建线程的例子");

    ThreadTest testObj=new ThreadTest();

    testObj.create();

    }

    public void create()

    {

    if(thrObj= =null)

    {

    thrObj=new Thread(this,"myThread");

    ______

    }

    }

    public void run()

    {

    System.out.println("Thread"+throbj.getName()+":"+"在运行!");

    }

    }


    正确答案:thrObj.start();
    thrObj.start(); 解析:本题考查考生对如何通过实现Runnable接口创建线程及使用线程的掌握,这是一道简单应用题。程序中,ThreadTest类实现了Runnable接口,所以ThreadTest类必须重写Runnable接口中的run()方法。在create()方法创建了一个名为thrObj的线程,并调用Thread类中的start()方法启动该线程。语句thrObj=new Thread(this,"myThread");中,this是Thread构造方法的第一个参数,作为该线程的目标对象,它必须实现Runnable接口。第二个参数 myTread为线程的名称。在这种构造方法下,线程thrObj就以目标对象中的run()方法作为自己的run()方法,本例中,目标对象就是ThreadTest类的对象。

  • 第3题:

    当一个线程处于new状态时,通过什么方法使其进入runnable状态?


    正确答案:
     

  • 第4题:

    下列程序通过实现Runnable接口创建一个线程,选择正确的语句填入程序的横线处。 class MyRun implements Runnable { String str; MyRun(String s) { str = s; } public void run() System.out.println(str); } } public class ex40 { public static void main(String[] args) { String name = "实现阶段Runnable 接口"; MyRun my = new MyRun(name); Thread th = th. start ( ); } }

    A.new MyRun(my)

    B.new Thread()

    C.new Thread(my)

    D.Thread(my)


    正确答案:C

  • 第5题:

    public class Threads5 {  public static void main (String[] args) {  new Thread(new Runnable() {  public void run() {  System.out.print(”bar”);  } }).start();  }  }  What is the result?() 

    • A、 Compilation fails.
    • B、 An exception is thrown at runtime.
    • C、 The code executes normally and prints “bar”.
    • D、 The code executes normally, but nothing prints.

    正确答案:C

  • 第6题:

    public class Threads 1 {  intx=0;  public class Runner implements Runnable {  public void run() {  int current = 0;  for(int=i=0;i<4;i++){  current = x;  System.out.print(current + “, “);  x = current + 2;  }  }  }  public static void main(String[] args) {  new Threads1().go();  }  public void go() {  Runnable r1 = new Runner();  new Thread(r1).start();  new Thread(r1 ).start();  }  }  Which two are possible results?()

    • A、 0, 2, 4, 4, 6, 8, 10, 6,
    • B、 0, 2, 4, 6, 8, 10, 2, 4,
    • C、 0, 2, 4, 6, 8, 10, 12, 14,
    • D、 0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,
    • E、 0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,

    正确答案:A,C

  • 第7题:

    public class Threads4 {  public static void main (String[] args) {  new Threads4().go();  }  public void go() {  Runnable r = new Runnable() { public void run() {  System.out.print(”foo”);  }  };  Thread t = new Thread(r);  t.start();  t.start();  }  }  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

  • 第8题:

    单选题
    Given that a static method doIt() in a class Work represents work to be done, what block of code will succeed in starting a new thread that will do the work?   CODE BLOCK a:   Runnable r = new Runnable() {   public void run() {   Work.doIt();   }   };   Thread t = new Thread(r);   t.start();   CODE BLOCK b:   Thread t = new Thread() {  public void start() {   Work.doIt();  }  };   t.start();   CODE BLOCK c:   Runnable r = new Runnable() {   public void run() {   Work.doIt();   }   };   r.start();  CODE BLOCK d:   Thread t = new Thread(new Work());   t.start();   CODE BLOCK e:   Runnable t = new Runnable() {   public void run() {   Work.doIt();   }   };   t.run();
    A

    Code block a.

    B

    Code block B.

    C

    Code block c.

    D

    Code block d.

    E

    Code block e.


    正确答案: A
    解析: 暂无解析

  • 第9题:

    多选题
    public class Threads 1 {  intx=0;  public class Runner implements Runnable {  public void run() {  int current = 0;  for(int=i=0;i<4;i++){  current = x;  System.out.print(current + “, “);  x = current + 2;  }  }  }  public static void main(String[] args) {  new Threads1().go();  }  public void go() {  Runnable r1 = new Runner();  new Thread(r1).start();  new Thread(r1 ).start();  }  }  Which two are possible results?()
    A

    0, 2, 4, 4, 6, 8, 10, 6,

    B

    0, 2, 4, 6, 8, 10, 2, 4,

    C

    0, 2, 4, 6, 8, 10, 12, 14,

    D

    0, 0, 2, 2, 4, 4, 6, 6, 8, 8, 10, 10, 12, 12, 14, 14,

    E

    0, 2, 4, 6, 8, 10, 12, 14, 0, 2, 4, 6, 8, 10, 12, 14,


    正确答案: A,E
    解析: 暂无解析

  • 第10题:

    单选题
    当使用SomeThread t=new SomeThread()创建一个线程时,下列叙述中正确的是(  )。
    A

    SomeThread类是包含run()方法的任意java类

    B

    SomeThread类一定要实现Runnable接口

    C

    SomeThread类是Thread类的子类

    D

    SomeThread类是Thread类的子类并且要实现Runnable接口


    正确答案: D
    解析:
    要创建线程有两种方式,可以继承Thread类或者实现Runnable接口。在程序中“someThread t=new someThread();”这一语句可以看出t是继承了Thread类。若为“Thread t=new Thread(new someThread());”则选择B项,Thread类的构造方法需要一个实现了Runnable接口的对象作为参数。

  • 第11题:

    多选题
    Which two can be used to create a new Thread?()
    A

    Extend java.lang.Thread and override the run method.

    B

    Extend java.lang.Runnable and override the start method.

    C

    Implement java.lang.thread and implement the run method.

    D

    Implement java.lang.Runnable and implement the run method.

    E

    Implement java.lang.Thread and implement the start method.


    正确答案: D,E
    解析: 暂无解析

  • 第12题:

    单选题
    class MyThread extends Thread {  public void run() { System.out.println(“AAA”); }  public void run(Runnable r) { System.out.println(“BBB”); }  public static void main(String[] args) {  new Thread(new MyThread()).start();  }  }   What is the result?()
    A

     AAA

    B

     BBB

    C

     Compilation fails.

    D

     The code runs with no output.


    正确答案: A
    解析: 暂无解析

  • 第13题:

    请完成下列Java程序:程序的功能演示了如何通过实现Runnable接口创建线程对象,程序中定义了一个类B,类中重写了含一个字符串参数的构造方法,并实现了Runnable接口,即在类B中编写了接口中的run()方法的方法体。还定义了一个应用程序类ex35_2,其中创建类B的3个对象b1,b2和b3作为线程对象t1,t2和t3的参数,并启动这3个线程。

    注意:请勿改动main()主方法和其他已有语句内容,仅在下划线处填入适当的语句。

    程序运行结果如下:

    public class ex35_2

    {

    public static void main(String args[ ])

    {

    Runnable b1=new B("First");

    Runnable b2=new B("Second");

    Runnable b3=new B("Third");

    Thread t1=new Thread(b1);

    Thread t2=new Thread(b2);

    Thread t3=new Thread(b3);

    t1.start ();

    t2.start ();

    t3.start();

    }

    }

    class B _____________________ Runnable

    {

    String s;

    public B(String str)

    {

    s=str;

    }

    _________________

    {

    for(int i=1;i<3;i++)

    {

    System. out. println ( s+ "运行!");

    try

    {

    Thread.sleep((int) (Math.random() *100) );

    }

    catch (InterruptedException e)

    {

    e.printStackTrace ( );

    }

    }

    System. out.println (s+"结束!");

    }

    }


    正确答案:implements public void run()
    implements public void run() 解析:本题主要考查Java中有关线程的基本操作。解题关键是要熟练掌握有关线程的基本知识。在Java中创建线程有两种基本方法:(1)是通过实现Runnable接口创建线程。(2)通过继承Thread类创建线程。第1个空,应该填implements关键字,其功能是实现Runnable接口。第2个空,应该填public void run(),其功能是重写run()方法。

  • 第14题:

    如果使用Thread t=new Test()语句创建一个线程,则下列叙述正确的是

    A.Test类一定要实现Runnable接口

    B.Test类一定是Thread类的子类

    C.Test类一定是Runnable的子类

    D.Test类一定是继承Thread类并且实现Runnable接口


    正确答案:B
    解析:本题考查线程的使用。Java中可以通过实现Runnable接口来创建线程。通过这种方式创建线程是把Runnable的一个对象作为参数传递给Thread类的一个构造方法,该对象提供线程体run()。如果题目中Test实现Runnable接口,则创建线程的方法是Thread t=new Thread(new Test())。Java中另一种创建线程的方法是通过继承 Thread类,重写其中的run()方法定义线程体,然后直接创建该子类的对象即可创建线程。题目中使用Thread t=new Test()语句创建线程,其中直接创建Test类对象,可知该类一定是继承Thread类。因此,本题的正确答案是选项B。

  • 第15题:

    阅读下面程序 public class Test implements Runnable { public static void main(String[] args) { ______ t.start(); } public void run() { System.out.println("Hello!"); } } 程序中下画线处应填入的正确选项是

    A.Test t=new Test();

    B.Thread t=new Thread();

    C.Thread t=new Thread(new Test());

    D.Test t=new Thread();


    正确答案:C

  • 第16题:

    class MyThread extends Thread {  public void run() { System.out.println(“AAA”); }  public void run(Runnable r) { System.out.println(“BBB”); }  public static void main(String[] args) {  new Thread(new MyThread()).start();  }  }   What is the result?()  

    • A、 AAA
    • B、 BBB
    • C、 Compilation fails.
    • D、 The code runs with no output.

    正确答案:A

  • 第17题:

    Which two code fragments will execute the method doStuff() in a separate thread?()

    • A、 new Thread() { public void run() { doStuff(); } }
    • B、 new Thread() { public void start() { doStuff(); } }
    • C、 new Thread() { public void start() { doStuff(); } } .run();
    • D、 new Thread() { public void run() { doStuff(); } } .start();
    • E、 new Thread(new Runnable() { public void run() { doStuff(); } } ).run();
    • F、 new Thread(new Runnable() { public void run() { doStuff(); } }).start();

    正确答案:D,F

  • 第18题:

    现有:  class ThreadBoth extends Threaa implements Runnable  {      public void run()  (System.out.print("hi");  }     public static voicl main (String  []  args)  {     Thread tl=new ThreadBoth():      Thread t2 = new Thread (tl):     tl.run():      t2.run():     }          结果为:()      

    • A、 hi hi
    • B、 hi
    • C、编译失败
    • D、运行时异常被抛出

    正确答案:A

  • 第19题:

    Which two code fragments will execute the method doStuff() in a separate thread?()

    • A、new Thread() {public void run() { doStuff(); }};
    • B、new Thread() {public void start() { doStuff(); }};
    • C、new Thread() {public void start() { doStuff(); }}.run();
    • D、new Thread() {public void run() { doStuff(); }}.start();
    • E、new Thread(new Runnable() {public void run() { doStuff(); }}).start();

    正确答案:D,E

  • 第20题:

    单选题
    public class Threads4 {  public static void main (String[] args) {  new Threads4().go();  }  public void go() {  Runnable r = new Runnable() { public void run() {  System.out.print(”foo”);  }  };  Thread t = new Thread(r);  t.start();  t.start();  }  }  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
    解析: 暂无解析

  • 第21题:

    单选题
    Runnable r = new Runnable() {  public void run() {  System.out.print(”Cat”);  }  };  Threadt=new Thread(r) {  public void run() {  System.out.print(”Dog”);  }  };  t.start();  What is the result?()
    A

     Cat

    B

     Dog

    C

     Compilation fails.

    D

     The code runs with no output.

    E

     An exception is thrown at runtime.


    正确答案: D
    解析: 暂无解析

  • 第22题:

    多选题
    Which two code fragments will execute the method doStuff() in a separate thread?()
    A

    new Thread() { public void run() { doStuff(); } }

    B

    new Thread() { public void start() { doStuff(); } }

    C

    new Thread() { public void start() { doStuff(); } } .run();

    D

    new Thread() { public void run() { doStuff(); } } .start();

    E

    new Thread(new Runnable() { public void run() { doStuff(); } } ).run();

    F

    new Thread(new Runnable() { public void run() { doStuff(); } }).start();


    正确答案: D,F
    解析: 暂无解析

  • 第23题:

    单选题
    public class Threads5 {  public static void main (String[] args) {  new Thread(new Runnable() {  public void run() {  System.out.print(”bar”);  } }).start();  }  }  What is the result?()
    A

     Compilation fails.

    B

     An exception is thrown at runtime.

    C

     The code executes normally and prints “bar”.

    D

     The code executes normally, but nothing prints.


    正确答案: B
    解析: 暂无解析