public class Threads3 implements Runnable {  public void run

题目
单选题
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”.

如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

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

A) class Test implements Runnabte{

public void run(){}

public void someMethod(){}

B) class Test implements Rnuuable{

public void run();

}

C) class Test implements Rnuuable{

public void someMethod();

}

D) class Test implements Rnuuable{

public void someMethod();{}

}

A.

B.

C.

D.


正确答案:A

第2题:

在下面程序的下画线处应填入的选项是 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接口创建线程。

第3题:

下面程序创建了一个线程并运行,请填空,使程序完整。

public class ThreadTest {

public static void main (String[] args) {

Hello h=Hew Hello ();

【 】

t.start ();

}

}

class Hello implements Runnable {

int i;

public void run () {

while(true) {

System.out.println("Hello" +i++);

if(i==5) break;

}

}

}


正确答案:Threadt=new Thread(h);
Threadt=new Thread(h); 解析:在通过实现Runnable接口来创建线程以后,该线程的启动将使得对象的run ()方法被调用。题目中缺少线程创建的语句,因此应该填写Thread t=new Thread(h);,该语句创建 Hello类的实例对象h的线程。

第4题:

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

第5题:

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

第6题:

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

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

第7题:

下列关于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类的构造函数。

第8题:

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

public class ThreadTest

{

public static void main(String args [])

{

Thread testObj1 = new Thread (new Hello ());

Thread testObj2 = new Thread (new Hello ());

testObj 2.start ( );

}

}

class Hello implements Runnable

{

int j;

public void run()

{

System.out.println("Hello" + j ++);

}

}


正确答案:testObj 1.start();
testObj 1.start();

第9题:

( 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

第10题:

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

更多相关问题