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是错误的,因为线程在睡眠时,将继续保护它的锁;
Exercise of Thread(1)
最新推荐文章于 2024-08-02 20:20:23 发布