niusouti.com

单选题class Thread2 implements Runnable {   void run() {    System.out.print("go ");    }   public static void main(String [] args) {   Thread2 t2 = new Thread2();   Thread t = new Thread(t2);   t.start();    }    }    结果为:()AgoB编译失败C代码运行,无输出结果D运行时异常被抛出

题目
单选题
class Thread2 implements Runnable {   void run() {    System.out.print("go ");    }   public static void main(String [] args) {   Thread2 t2 = new Thread2();   Thread t = new Thread(t2);   t.start();    }    }    结果为:()
A

 go

B

 编译失败

C

 代码运行,无输出结果

D

 运行时异常被抛出


相似考题
更多“class Thread2 implements Runnable {   void run() {    System”相关问题
  • 第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题:

    下列关于Test类的定义中,正确的是( )。

    A.class Test implements Runnable{ public void run{} Dublic void someMethod[]{} }

    B.class Test implements Runnable( puIblic void run; }

    C.class Test implements Runnable( Dublic void someMethod[]; }

    D.class Test implements Runnable( public void someMethod{} }


    正确答案:A
    A。【解析】java中实现多线程的方法之一就是实现Runnable接口中的run方法,把实现Runnable接口的子类对象传递给Thread类的构造函数。

  • 第4题:

    ( 30 )在程序的下划线处应填入的选项是

    public class Test _________{

    public static void main(String args[]){

    Test t = new Test();

    Thread tt = new Thread(t);

    tt.start();

    }

    public void run(){

    for(int i=0;i<5;i++){

    system.out.println( " i= " +i);

    }

    }

    }

    A ) implements Runnable

    B ) extends Thread

    C ) implements Thread

    D ) extends Runnable


    正确答案:A

  • 第5题:

    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

  • 第6题:

    public class Threads2 implements Runnable {  public void nun() {  System.out.println(”run.”);  throw new RuntimeException(”Problem”);  }  public static void main(String[] args) {  Thread t = new Thread(new Threads2());  t.start();  System.out.println(”End of method.”);  }  }  Which two can be results?()

    • A、 java.lang.RuntimeException: Problem
    • B、 run.    java.lang.RuntimeException: Problem
    • C、 End of method.    java.lang.RuntimeException: Problem
    • D、 End of method.      run.      java.lang.RuntimeException: Problem
    • E、 run.    java.lang.RuntimeException: Problem     End of method.

    正确答案:D,E

  • 第7题:

    下列代码正确的是哪项?() 

    • A、 public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }
    • B、 public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }
    • C、 public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }
    • D、 public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }

    正确答案:C

  • 第8题:

    class Work implements Runnable {  Thread other;   Work(Thread other) { this.other = other; }  public void run() {  try { other.join(); } catch (Exception e) { }  System.out.print("after join ");  } }  class Launch {  public static void main(String [] args) {  new Thread(new Work(Thread.currentThread())).start();  System.out.print("after start ");  } }  结果为:()

    • A、after join
    • B、after start
    • C、after join after start
    • D、after start after join

    正确答案:D

  • 第9题:

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

     Compilation fails.

    B

     An exception is thrown at runtime.

    C

     The code executes and prints “running”.

    D

     The code executes and prints “runningrunning”.

    E

     The code executes and prints “runningrunningrunning”.


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

  • 第10题:

    多选题
    class Order implements Runnable {    public void run ()  {  try { Thread.sleep (2000) ;  } catch (Exception e)    System.out.print("in") ;  public static void main (String [] args)  {    Thread t = new Thread (new Order ()) ;    t.start () ;  System.out.print ("pre ") ;  try { t.join () ;  } catch (Exception e)  { }    System.out.print ("post") ;   可产生哪两项结果?()
    A

    pre in post

    B

    pre in

    C

    in post pre

    D

    in pre post

    E

    pre post in


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

  • 第11题:

    多选题
    class Order implements Runnable {  public void run() {  try { Thread.sleep(2000); } catch (Exception e) { }  System.out.print("in ");  }  public static void main(String [] args) {  Thread t = new Thread(new Order());  t.start();  System.out.print("pre ");  try { t.join(); } catch (Exception e) { }  System.out.print("post ");  } }  可产生哪两项结果?()
    A

    in pre

    B

    pre in

    C

    in pre post

    D

    pre in post


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

  • 第12题:

    单选题
    现有:  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

    运行时异常被抛出


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

  • 第13题:

    下列哪个方法可用于创建一个可运行的类? ( )

    A.public class X implements Runable {public void run(){...,.,}}

    B.public class X implements Thread {public void run(){......}}

    C.public class X implements Thread {public int run(){……}}

    D.public class X implements Runable {protected void run(){.....}}


    正确答案:A

  • 第14题:

    在下面程序的下画线处应填入的选项是 public class Test______{ public static void main(String args[]) { Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run() { for(int i=0;i<5;i++) System.out.println("i="+i); } }

    A.implements Runnable

    B.extends Thread

    C.implements Thread

    D.extends Runnable


    正确答案:A
    解析:创建线程有两种方法:实现java.lang.Runnahle接口和继承Thread类并重写run()方法。从创建线程实例的语句Thread tt=new Thread(t);可以看出本程序将Test类的实例t作为参数传递给Thread类的构造方法,因此是通过实现Runnable接口创建线程。

  • 第15题:

    下列程序通过实现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

  • 第16题:

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

    • A、 Compilation fails.
    • B、 An exception is thrown at runtime.
    • C、 The code executes and prints “running”.
    • D、 The code executes and prints “runningrunning”.
    • E、 The code executes and prints “runningrunningrunning”.

    正确答案:E

  • 第17题:

    public class Foo implements Runnable (  public void run (Thread t) {  system.out.printIn(“Running.”);  }  public static void main (String[] args)  {  new thread (new Foo()).start();  } )   What is the result?()      

    • A、 An exception is thrown.
    • B、 The program exists without printing anything.
    • C、 An error at line 1 causes compilation to fail.
    • D、 An error at line 2 causes the compilation to fail.
    • E、 “Running” is printed and the program exits.

    正确答案:D

  • 第18题:

    现有:   class Thread2 implements Runnable {   void run() {   System.out.print("go ");   }   public static void main(String [] args) {   Thread2 t2 = new Thread2();   Thread t = new Thread(t2);   t.start();   }   }   结果为:()

    • A、 go
    • B、 编译失败
    • C、 代码运行,无输出结果
    • D、 运行时异常被抛出

    正确答案:B

  • 第19题:

    Which method must be defined by a class implementing the java.lang.Runnable interface? () 

    • A、 void run()
    • B、 public void run()
    • C、 public void start()
    • D、 void run(int priority)
    • E、 public void run(int priority)
    • F、 public void start(int priority)

    正确答案:B

  • 第20题:

    现有:  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

  • 第21题:

    单选题
    下列代码正确的是哪项?()
    A

     public class Session implements Runnable, Clonable{   public void run ();public Object clone () ; }

    B

     public class Session extends Runnable, Cloneable {  public void run() {/*dosomething*/}       public Object clone() {/*make a copy*/} }

    C

     public abstract class Session implements Runnable, Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}        }

    D

     public class Session implements Runnable, implements Clonable {       public void run() {/*do something*/}       public Object clone() {/*make a copy*/}       }


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

  • 第22题:

    单选题
    class ThreadBoth extends Thread implements Runnable {  public void run(){ System.out.print("hi "); }  public static void main(String [] args){  Thread t1 = new ThreadBoth();   Thread t2 = new Thread(t1);  t1.run();  t2.run();  }  }  结果为:()
    A

    hi

    B

    hi hi

    C

    编译失败

    D

    代码运行,但无输出结果


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

  • 第23题:

    单选题
    class Thread2 implements Runnable {   void run() {    System.out.print("go ");    }   public static void main(String [] args) {   Thread2 t2 = new Thread2();   Thread t = new Thread(t2);   t.start();    }    }    结果为:()
    A

     go

    B

     编译失败

    C

     代码运行,无输出结果

    D

     运行时异常被抛出


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

  • 第24题:

    单选题
    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
    解析: 暂无解析