分享
 
 
 

Exercise of the Thread(1)

王朝java/jsp·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

1.Given the following

1.class MyThread extends Thread {

2.

3. public static void main(String []args){

4. MyThread t = new MyThread();

5. t.run();

6. }

7.

8. public void run(){

9. for(int i=1;i<3;++i){

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

11. }

12. }

13.}

what is the result?

A. This code is not compile due to line 4.

B. This code is not compile due to line 5.

C. 1..2..

D. 1..2..3..

E. An exception is thrown at runtime.

(c).在第5行,程序直接call run()method了,所以虽然没有start(),run()还是被执行了,不会产生编译错误;

2.Which two of the following methods are defined in class Thread?

A. start()

B. wait()

C. notify()

D. run()

E. terminate()

(A.D)在Thread类里,被定义的只有start()和run(),wait()和notify()是java.lang.object里的方法,而terminate()完全和线程类毫无关系;

3.The following block of code creates a Thread using a Runnable target:

runnable target = new runnable();

Thread myThread = new Thread(target);

Which of the following classes can be used to create the target,so that the preceding code compiles correctly?

A. public class MyRunnable extends Runnable{public void run(){}}

B. public class MyRunnable extends Object{public void run(){}}

C. public class MyRunnable implements Runnable {public void run(){}}

D. public class MyRunnable implements Runnable {void run(){}}

E. public class MyRunnable implements Runnable {public void start(){}}

(c).接口必须implements而不能extends, run()方法必须是public的, 接口的执行方法是run()而不是start();

4.Given the following:

1.class MyThread extends Thread {

2.

3. public static void main(String []args){

4. MyThread t = new MyThread();

5. t.start();

6. System.out.println("one. ");

7. t.start();

8. System.out.println("two. ");

9. }

10.

11. public void run(){

12. System.out.println("Thread ");

13. }

14.}

what is the result of this code?

A. Compilation fails.

B. An exception occurs at runtime.

C. Thread one. Thread two.

D. The output cannot be determined.

(B).编译通过,但在star()方法开始后,将抛出一个IllegalThreadStateException

5.Given the following:

1.public class MyRunnable implements Runnable(){

2. public void run() {

3. //some code here

4. }

5.}

which of these will create and start this thread?

A. new Runnable(MyRunnable).start();

B. new Thread (MyRunnable).run();

C. new Thread (new MyRunnable()).start();

D. new MyRunnable().start();

(c). D不正确,因为MyRunnable里没有start()method,而且只有start()方法,才可以启动线程;把一个MyRunnable的实例传给线程的构造器的同时,线程就会启动;类或接口的名字,不能给任何构造器使用;

6.Given the following:

1.class MyThread extends Thread{

2.

3. public static void main(String []args){

4. MyThread t = new MyThread();

5. Thread x = new Thread(t);

6. x.start();

7. }

8.

9. public void run(){

10. for(int i=0;i<3;i++){

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

12. }

13. }

14.}

which is the result of this code?

A. Compilation fails.

B. 1..2..3..

C. 0..1..2..3..

D. 0..1..2..

E. An exception occurs at runtime.

(D). Thread类implements Runnable接口,所以,在第5行线程可以将一个MyThread类的对象,放在构造器里使用;

7.Given the following .

1.class Test{

2.

3. public static void main(String []args){

4. printAll(args);

5. }

6.

7. public static void printAll(String []lines){

8. for(int i=0;i<lines.length;i++){

9. System.out.println(lines[i]);

10. Thread.currentThread().sleep(1000);

11. }

12. }

13 }

the static method Thread.currentThread() returns a reference to the currently executing Thread object.What is the result of this code??

A. Each String in the array lines will output , with a 1-secnod pause.

B. Each String in the array lines will output , with no pause in between because this method is not executed in a Thread.

C. Each String in the array lines will output , and there is no guarantee there will be a pause beacuse CurrentThread() may not retrieve this method.

D. This code will not compile.

(D).sleep()方法必须附加在try\catch块里,或者方法printAll()必须声明它抛出一个InterruptedException.

8.Assume you have a class that holds two private variables:

a and b.Which of the following pairs can prevent concurrent access problems in that class?(Choose all that apply).

A. public int read(int a,int b){return a+b;}

public void set (int a,int b){this.a=a;this.b=b;}

B. public synchronized int read(int a,int b){return a+b;}

public synchronized void set(int a,int b){this.a=a;this.b=b;}

C. public int read(int a,int b){synchronized(a){return a+b;}}

public void set(int a,int b){synchronized(a){this.a=a;this.b=b;}}

D. public int read(int a,int b){synchronized(a){return a+b;}}

public void set(int a,int b){synchronized(b){this.a=a;this.b=b;}}

E. public synchronized(this) int read(int a,int b){return a+b;}

public synchronized(this) void set(int a,int b){this.a=a;this.b=b;}

F. public int read(int a,int b){synchronized(this){return a+b;}}

public void set(int a,int b){synchronized(this){this.a=a;this.b=b;}}

(B.F).用synchronized方法做了标记的,在这个对象进行前,线程将获得一个锁,只有一个线程可以在瞬间任意的设置和读取数据,因此它可以保证read()方法总是返回一对有效的数据。

9.Which class or interface defines the wait(),notify(),and notifyAll() methods?

A. Object

B. Thread

C. Runnable

D. Class

(A). Java Doc

10.Which two are true?

A. A static method cannot be synchronized.

B. If a class has synchronized code,multiple threads can still access the nonsynchronized code.

C. Variables can be protected from concurrent access problems by marking them with the synchronized keyword.

D. When a thread sleeps,it releases tis locks.

E. When a thread invokes wait(),it releases its locks.

(B.E). B:因为多线程允许非synchronized代码进入,甚至在类里面他可以有同样的synchronized方法; E:因为wait()方法在调用时,会放弃它的锁; A是错误的,因为静态方法可以是synchronized的; C是错误的,因为只有方法没有变量,可以是同步的; D是错误的,因为线程在睡眠时,将继续保护它的锁;

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有