多选题Which two code fragments will execute the method doStuff() in a separate thread?()Anew Thread() {public void run() { doStuff(); }};Bnew Thread() {public void start() { doStuff(); }};Cnew Thread() {public void start() { doStuff(); }}.run();Dnew Thread()

题目
多选题
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();

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

第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题:

下列程序的执行结果是______。 class A5 extends Thread { boolean b; A5 (boolean bb) { b = bb; } public void run() { System.out.println(this.getName() + "运行"); } } public class Testl5 { public static void main(String[] args) { A5 a1 = new A5(true); A5 a2 = new A5(false); if(a1.b) A1.start(); if (a2 .b) A2.start(); } }

A.Thread-0

B.Thread-1

C.Thread-0

D.Thread-1 Thread-1 Thread-0


正确答案:A
解析:类A5继承了Thread类,并且重写了Thread类的run()方法,调用本线程的getName()方法打印出系统给本线程定义的名称。在main()方法中,a1和a2是A5的对象,它们对应的系统默认的线程名称分别是Thread—0和Thread—1,根据类A5的类变量b的布尔值控制哪一个线程调用start()方法,这里应该是a1线程被调度执行。

第3题:

( 24 )请阅读下面程序

public class ThreadTest {

public static void main ( String args[ ]){

Thread t1 = new Thread ( new Hello ()):

Thread t2 = new Thread ( new Hello ()):

t l .start ():

t2.start ();

class Hello implements Runnable {

int i ;

public void run (){

while ( true ) {

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

if ( i=5 ) break :

}

该程序创建线程使用的方法是()

A )继承 Thread 类

B )实现 Runnable 接口

C ) t l.start ()

D ) t2.start ()


正确答案:B

第4题:

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

第5题:

请阅读下面程序,说明该程序创建线程使用的方法是( )。 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接口。

第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题:

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

第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题:

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

第10题:

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

更多相关问题