1. class A implements Runnable (  2. int i;  3. public void 

题目
单选题
1. class A implements Runnable (  2. int i;  3. public void run ( ) (  4. try (  5. thread.sleep(5000);  6. i= 10;  7. ) catch(InterruptedException e) {}  8. )  9. )  10.    11. public class Test {  12. public static void  main (string args[]) ( 13. try (  14. A a = new A ( );  15. Thread t = new Thread (a);  16. t.start( );  17.    18. int j= a.i;  19.    20. ) catch (Exception e) {}  21. )  22. }   Which statement al line 17 will ensure that j=10 at line 19?()
A

 a.wait();

B

 t.wait();

C

 t.join();

D

 t.yield();

E

 t.notify();

F

 a.notify();

G

 t.interrupt();

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

第1题:

通过实现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();

第2题:

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

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

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}


正确答案:

 

错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

以通过A.x 来明确。

第5题:

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

第6题:

下列关于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

第7题:

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

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

第8题:

( 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

第9题:

选择正确的语句填在下列程序的横线处,使程序正常运行。 package ch1; import java. io. *; import j ava. util. *; class C45 implements Seritizable ______Public Thread t = new Thread(new T45(), "t"); public intcnt = 0; public C45() { t. start ( ) class T45 implements Runnable public int[] a = new int[4]; public void run() { for(int i - 0; i < 4; i++) { a[i] - i +4; } } } public class ex45 { static String fileName = "ch1\\file45.txt"; public static void main(String[] args) throws Exception { C45 bj = new C45 (); FileOutputStream fos = new FileOutputStream(fileName); ObjectOutputStream os = new ObjectOutputStream(fos); oos. writeObject (obj); oos.clese (); fos.close (); System. out, println (obj . toString ( ) ); } }

A.transient

B.protected

C.package

D.final


正确答案:A

第10题:

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

更多相关问题