public class A extends Thread {  A() {  setDaemon(true);  } 

题目
单选题
public class A extends Thread {  A() {  setDaemon(true);  }  public void run() {  (new B()).start();  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“A done”);  }  class B extends Thread {  public void run() {  try {  Thread.sleep(60000);  } catch (InterruptedException x) {}  System.out.println(“B done”);  }  }  public static void main(String[] args) {  (new A()).start();  }  }   What is the result?()
A

 A done

B

 B done

C

 A done B done

D

 B done A done

E

 There is no exception that the application will print anything.

F

 The application outputs “A done” and “B done”, in no guaranteed order.

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

第1题:

下列程序的执行结果是______。 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线程被调度执行。

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

下面程序的功能是创建一个显示5个“Hello!”的线程并启动运行。请将程序补充完整。

public class ThreadTest extends Thread {

public static void main(String args[]) {

ThreadTest t=new ______;

t.start();

}

public void run() {

int i=0;

while(true) {

System.out.println("Hello!");

if(i++==4)break;

}

}

}


正确答案:ThreadTest()
ThreadTest()

第4题:

public class SomeException {  } Class a:  public class a {  public void doSomething() { }  } Class b:  public class b extends a {  public void doSomething() throws SomeException { }  }  Which is true about the two classes?() 

  • A、 Compilation of both classes will fail.
  • B、 Compilation of both classes will succeed.
  • C、 Compilation of class a will fail. Compilation of class b will succeed.
  • D、 Compilation of class a will fail. Compilation of class a will succeed.

正确答案:D

第5题:

阅读下面程序 public class Test2 ______ { public static void main(String[] args) { Thread t=new Test2(); t.start(); } public void run() { System.out.println("How are you."); } } 程序中下画线处应填入的正确选项是

A.implements Thread

B.extends Runnable

C.implements Runnable

D.extends Thread


正确答案:D

第6题:

阅读下面程序 public class Test2______ { public static void main(String[] args){ Thread t=new Test2(); t. start(); } public void run(){ System. out. priatln("How are you. "); } } 在程序下画线处填入的正确的选项是

A.implements Thread

B.extends Runnable

C.implements Runnable

D.extends Thread


正确答案:D
解析:Thread类是多线程基类,多线程启动类必须继承此类。而实现Runnable接口的类能作为多线程的一个执行任务,一般作为参数传给新的Thread类。

第7题:

下列程序创建了一个线程并运行,横线处应填入的正确代码是( )。 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资源时,就可以脱离创建它的主线程独立地开始自己的生命周期了。

第8题:

Given:Which two, inserted at line 11, will allow the code to compile?()

A.public class MinMax<?> {

B.public class MinMax<? extends Number> {

C.public class MinMax<N extends Object> {

D.public class MinMax<N extends Number> {

E.public class MinMax<? extends Object> {

F.public class MinMax<N extends Integer> {


参考答案:D, F

第9题:

下列程序创建了一个线程并运行,请在下划线处填入正确代码。

public class Try extends Thread{

public static void main(String args[]){

Threadt=new Try();

【 】;

}

public void run(){

System.out.println(“Try!”);

}

}


正确答案:i
i

第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

更多相关问题