t 是一个合法的 Thread 对象的引用,并且 t 的合法 run() 方法如下:  public void run() {  System.out.print("go ");  }  及:   t.start();  t.start();  t.run();  哪一个是结果?() A、goB、go goC、go go goD、go 之后跟着一个异常

题目

t 是一个合法的 Thread 对象的引用,并且 t 的合法 run() 方法如下:  public void run() {  System.out.print("go ");  }  及:   t.start();  t.start();  t.run();  哪一个是结果?() 

  • A、go
  • B、go go
  • C、go go go
  • D、go 之后跟着一个异常

相似考题
更多“t 是一个合法的 Thread ”相关问题
  • 第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题:

    现有:t是一个合法的Thread对象的引用,并且t的合法run()方法如下:publicvoidrun(){System.out.print("go");}及:t.start();t.start();t.run();哪一个是结果?()

    A.gogo

    B.gogogo

    C.go之后跟着一个异常

    D.gogo之后跟着一个异常


    参考答案:C

  • 第3题:

    下列程序通过设定线程优先级,抢占主线程的CPU,选择正确的语句填入横线处。 class T14 implements Runnable { private Boolean fStop - true; public void run() { while(fStop) { System.out.println(Thread.currentThread().getName() + "run"); try { Thread.sleep(l); } catch(Exception e) { e.printStackTrace(); } } } public void stopRun() { fStop = false; } } public class Testl4 { public static void main(String[] args) { T14 t14 = new T14(); Thread t1 = new Thread(ti4, "T14"); Thread t = Thread.currentThread()'; ______; Ti.start(); T14.stopRun(); System.out.println ( "stop "); } }

    A.setPriority(Thread. MIN_PRIORITY)

    B.t1 .setPriority(Thread. MIN_PRIORITY)

    C.t.setPfiofity(Thread. MIN_PRIORITY)

    D.t14.setPriority(Thread. MIN_PRIORITY)


    正确答案:C
    解析:使用setPriority方法来设定线程的优先级,程序中的t是主线程,t1是创建的线程,t6是实现了Runnable接口的类的实例,由于使用的参数是MIN_PRIORITY,因此是将主线程t的优先级设为最低,这样t1才能抢占t的CPU。

  • 第4题:

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

    A.Test t=flew Test()

    B.Thread t=new Thread();

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

    D.Test t=new Thread();


    正确答案:C
    解析:根据t. start()可知t应该是一个Thread类,排除A)。Thread类与Test类之间没有继承关系,所以排除D)。B)没有指定创建线程的对象,因此t. start()语句不能使Test类的run方法运行。所以选C)。

  • 第5题:

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

    A.Some Thread类是包含run( )方法的任意Java类

    B.Some Thread类-定要实现Runnable接口

    C.Some Thread类是Thread类的子类

    D.Some Thread类是Thread类的子类并且要实现Run-nable接口


    正确答案:C
    由SomeThreadt=newSomeThread()可知此题是通过继承Thread类来创建线程的。

  • 第6题:

    下列程序的功能是在监控台上每隔一秒钟显示一个字符串”Hello!”,能够填写在程序中下画线位置,使程序完整并能正确运行的语句是( )。 public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test: Threadt t=new Thread(t); tt.start: } public void run{ for(;;){ try{ ; }catch(e){} System.out.println("Hello"); } } }

    A.sleep(1000)

    B.t.sleep(1000)InterruptedExceptionInterruptedException

    C.Thread.sleep(1000)

    D.Thread.sleep(1000)RuntimeExceptionInterruptedException


    正确答案:D
    D。【解析】本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,Testt=newTest语句定义了Test的1个实例,Threadtt=newThread(t)定义了1个名为tt的线程,tt.start语句启动线程。通过try-catch语句来处理异常。try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep(1000)语句来实现,调用InterruptedException来完成。RuntimeException类包含有较多子类,比如算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常IndexOutOfBoundsException等。

  • 第7题:

    下列程序的输出结果是______。 class T44 implements Runnable { public void run() { System.out.print in (Thread.currentThread ( ).getName ( ) + "运行" ); } } public class ex44 { public static void main(String[] args) { Thread t1 = new Thread(new T44(), "t1"); Thread t2 = new Thread(new T44 () , "t2"); t1 .setPriority(Thread. MAX_PRIORITY); t2.setPriority(Thread.MIN_PRIORITY); t2. start (); t1 .start (); } }

    A.t1 运行 t2 运行

    B.t2 运行 t1 运行

    C.t1 运行 t1 运行

    D.t2 运行 t2 运行


    正确答案:A

  • 第8题:

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

  • 第9题:

    单选题
    Given that t1 is a reference to a live thread, which is true?()
    A

    The Thread.sleep() method can take t1 as an argument.

    B

    The Object.notify() method can take t1 as an argument.

    C

    The Thread.yield() method can take t1 as an argument.

    D

    The Thread.setPriority() method can take t1 as an argument.

    E

    The Object.notify() method arbitrarily chooses which thread to notify.


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

  • 第10题:

    单选题
    1. class MyThread implements Runnable {  2. public void run() {  3. System.out.print("go ");  4. }  5.  6. public static void main(String [] args) {  7. // insert code here  8. t.start(); 9. }  10. }  和如下四句:  Thread t = new MyThread(); MyThread t = new MyThread();  Thread t = new Thread(new Thread());  Thread t = new Thread(new MyThread());  分别插入到第5行,有几个可以通过编译?()
    A

    0

    B

    1

    C

    2

    D

    3


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

  • 第11题:

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

  • 第12题:

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

  • 第13题:

    t是一个合法的Thread对象的引用,并且t的合法run()方法如下:publicvoidrun(){System.out.print("go");}及:t.start();t.start();t.run();哪一个是结果?()

    A.go

    B.gogo

    C.gogogo

    D.go之后跟着一个异常


    参考答案:D

  • 第14题:

    阅读下面代码 public class Test implements Runnable { public void run(Thread t) { System.out.println("Running"); } public static void main(String[] args) { Thread tt=new Thread(new Test()); tt.start(); } } 代码运行的结果是

    A.将抛出一个异常

    B.没有输出并正常结束

    C.输出“Running”并正常结束

    D.程序第2行将出现一个编译错误


    正确答案:D

  • 第15题:

    下列程序的执行结果是______。 class T5 implements Runnable { public void run() { int i = 0; While(i<2) { Thread t = Thread.currentThread(); System.out.println(i + t.getName()); i++ } } } public class Testl6 { public static void main(String[] args) { Thread t1 = new Thread(new T5(), "TS"); t1.start (); Thread t = Thread.currentThread(); for(int i = 0; i<3; i++) { System.out.println(I + t.getName()); if(i == 1 && t1.isAlive()) t.yield(); } } }

    A.Omain lmam 0T5 1T5 2main

    B.Omain lmam 2main

    C.Omain lmain 2main 0T5 1T5

    D.Omain lmain 0T5 1T5


    正确答案:A
    解析:currentThread()方法得到main()方法的线程t,在循环中打印出t的名称,再判断如果t1还活着,就执行t.yield(), main()方法让出CPU,t1获得运行的机会,并执行t1的循环,输出2行包含n名字的字符串, t1执行完毕,回到main()方法中,线程main继续执行,输出最后一行字符串。

  • 第16题:

    如果使用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。

  • 第17题:

    下列程序创建了一个线程并运行,横线处应填入的正确代码是( )。 public class Try extends Thread{ public static void main(String args[]){ Thread t=new Try; ; } public void runf System.out.println(”Try!"); } }

    A.t.start

    B.t.class

    C.t.thread

    D.t.static


    正确答案:A
    A。【解析】start是类Thread的方法,其中start方法用于启动线程,使之从新建状态转入就绪状态并进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的主线程独立地开始自己的生命周期了。

  • 第18题:

    阅读下面程序 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

  • 第19题:

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

    A.继承Thread类

    B.实现Runnable接口

    C.tl.start

    D.t2.start


    正确答案:B
    B。【解析】本题考查线程的创建。在Java中,创建线程有两种方法:①通过实现Runnable接口创建线程。Runnable接口中只定义了一个run方法作为线程体。②通过继承Thread类创建线程,Thread类本身实现了Runnable接口。创建的新的线程不会自动运行,必须调用start方法才能运行。本题中HolloWorld类实现了Runnable接口。

  • 第20题:

    Given that t1 is a reference to a live thread, which is true?()

    • A、The Thread.sleep() method can take t1 as an argument.
    • B、The Object.notify() method can take t1 as an argument.
    • C、The Thread.yield() method can take t1 as an argument.
    • D、The Thread.setPriority() method can take t1 as an argument.
    • E、The Object.notify() method arbitrarily chooses which thread to notify.

    正确答案:E

  • 第21题:

    单选题
    t 是一个合法的 Thread 对象的引用,并且 t 的合法 run() 方法如下:  public void run() {  System.out.print("go ");  }  及:   t.start();  t.start();  t.run();  哪一个是结果?()
    A

    go

    B

    go go

    C

    go go go

    D

    go 之后跟着一个异常


    正确答案: 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 Test extends Thread{  public static void main(String[] args)  {    Thread t=new Thread();    t.start();  }  public void run()  {    System.out.println("Hello");  }}
    A

    程序不能通过编译,因为没有import语句将Thread类引入

    B

    程序不能通过编译,因为Test类没有实现Runnable接口

    C

    程序通过编译,且运行正常,没有任何输出

    D

    程序通过编译,且运行正常,打印出一个"Hello"


    正确答案: A
    解析:
    此程序继承了Thread,同时也有run方法,符合线程的创建规则,但是在创建线程对象时,所使用的的类为Thread,此处所创建对象毫无意义,不会运行任何结果,应该创建Test的对象,所以该程序会通过编译,且运行正常,但是没有任何输出。

  • 第24题:

    单选题
    现有:t是一个合法的Thread对象的引用,并且t的合法run()方法如下:  public void run()  {  System.out.print ("go");      }      及:  t.start();      t.start();      t.run();      哪一个是结果?()
    A

    go go

    B

    go go go

    C

    go之后跟着一个异常

    D

    go go之后跟着一个异常


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