SCJP考试中的线程题目分析 |
来源:www.chinajavaworld.net |
|
new Thread () { |
public void run() { |
synchronized(s1) { |
s2.append(“A”); |
synchronized(s2) { |
s2.append(“B”); |
System.out.print(s1); |
System.out.print(s2); |
} |
} |
} |
}.start(); |
new Thread() { |
public void run() { |
synchronized(s2) { |
s2.append(“C”); |
synchronized(s1) { |
s1.append(“D”); |
System.out.print(s2); |
System.out.print(s1); |
} |
} |
} |
}.start(); |
} |
} |
Which two statements are true? ( | Choose Two) |
A. The program prints “ABBCAD” |
B. The program prints “CDDACB” |
C. The program prints “ADCBADBC” |
D. The output is a n condition | on-deterministic point becau | se of a possible deadlock |
E. The output is dep is running on. | endent on the threading mode | l of the system the program |
Answer:DE |
3: |
What will happen whe | n you attempt to compile and | run the following code? |
public class Test{ |
int i = 0; |
public static void main(Strin | g argv[]) { |
Test t = new Test(); |
t.myMethod(); |
} |
public void myMethod(){ |
while(true) { |
try { |
wait(); |
}catch (InterruptedException e | ) {} |
i++; |
} |
} |
} |
A. Compile time erro | r, no matching notify within | the method. |
B. Compile and run b | ut an infinite looping of th | e while method. |
C. Compilation and run without a | ny output. |
E. Runtime Exception | "IllegalMonitorStatExceptio | n". |
Answer: E |
Note: The wait/notif synchronized. In this ca synchronized) and will t | y protocol can only be used se calling code does not hav hus cause an Exception at ru | within code that is e a lock on the object(not ntime. |
4: |
1.10 What is the result of compi | ling and executing the following code? |
public class ThreadT | est extends Thread { |
public void run() { |
System.out.println("In run"); |
yield(); |
System.out.println("Leaving run"); |
} |
public static void main(String | args []) { |
(new ThreadTest()).start(); |
} |
} |
A. The code fails to | compile in the main() metho | d. |
B. The code fails to | compile in the run() method | . |
C. Only the text "In run" will b | e displayed. |
D. The text "In run" | followed by "Leaving run" w | ill be displayed. |
E. The code compiles correctly, | but nothing is displayed. |
Answer: D |
5: |
1.12 Which of the following will wait()B. notify()C. yield()D. suspen | definitely stop a thread from executing?A. d()E. sleep()Answer: ACDE |
6: |
1.12 Which of the fo | llowing will definitely stop | a thread from executing? |
A. wait() |
B. notify() |
C. yield() |
D. suspend() |
E. sleep() |
Answer: ACDE |
7: |
Which of the follow | ing statements about threadi | ng are true? |
A. You can only obtain a mutuall extends Thread or implements runnabl | y exclusive lock on methods in a class that e. |
B. You can obtain a mutually exc | lusive lock on any object. |
C. You can't obtain | a mutually exclusive lock on | any object. |
D. Thread scheduling | algorithms are platform dep | endent. |
Answer: BD |
8: |
Consider the following statement: |
Thread myThread = new Thread(); |
Which of the followi | ng statements are true regar | ding myThread? |
A. The thread myThre | ad is now in a runnable stat | e. |
B. The thread myThre | ad has a priority of 5. |
C. On calling the start() method class will be executed. | on myThread, the run method in the Thread |
D. On calling the st class will be executed. | art() method on myThread, th | e run method in the calling |
Answer: C |
Note: the priority o the constructor. | f myThread will be inherited | from the Thread that called |
9: |
What is the effect of issuing a | wait() method on an object?(Mutiple) |
1) If a notify() met effect | hod has already been sent to | that object then it has no |
2) The object issuing the call t a notify() or notifyAll() method | o wait() will halt until another object sends |
3) An exception will be raised |
4) The object issuin with any other objects u | g the call to wait() will be sing the receiving object. | automatically synchronized |
ANSWER 1) |
10: |
Pick all the true statements below. |
1) If a thread wants object's lock. | to call wait() on an object | , the thread must own that |
2) There is a method that you ca that puts the instance to sleep for | n call on an instance of the Thread class a specified number of milliseconds. |
3) At the moment whe the object for which it | n a thread is notified, it a was waiting. | utomatically gets the lock of |
ANSWER 1 |
11: |
1. class Z { |
2. public static void main(Str | ing[] args) { |
3. new Z(); |
4. } |
5. |
6. Z() { |
7. Z alias1 = this; |
8. Z alias2 = this; |
9. synchronized(alias1) { |
10. try { |
11. alias2.wait(); |
12. System.out.println(" | DONE WAITING"); |
13. } |
14. catch (InterruptedException e) { |
15. System.o | ut.println("INTERRUPTED"); |
16. } |
17. catch (Exception e) { |
18. System.out.println(" | OTHER EXCEPTION"); |
19. } |
20. finally { |
21. System.out.println("FINALLY"); |
22. } |
23. } |
24. System.out.println("ALL DONE"); |
25. } |
26. } |
Mutiple: |
1) Compiler error. |
2) The application compiles but | doesn't print anything. |
3) The application prints "DONE | WAITING". |
4) The application prints "INTER | RUPTED". |
5) The application prints "OTHER | EXCEPTION". |
ANS: 2) |
12: |
What will happen wh | en you attempt to compile an | d run the following code? |
class MyThread extends Thread{ |
public void run(){ |
System.out.println | ("MyThread: run()"); |
} |
public void start(){////注意, | 此方法重写后,不再有CALL RUN()方法的功能。 |
System.out.println("MyThread: | start()"); |
} |
} |
class MyRunnable implements Runnable{ |
public void run(){ |
System.out.println("MyRunnable | : run()"); |
} |
public void start(){//Runnable 否则,将CALL THREAD 的start()执行run | 并无此方法,所以,除非本类的对象可以CALL 它, () |
System.out.println | ("MyRunnable: start()"); |
} |
} |
public class MyTest { |
public static void main(String | args[]){ |
MyThread myThread = new MyThread(); |
MyRunnable myRunnable = new My | Runnable(); |
Thread thread = | new Thread(myRunnable); |
myThread.start(); |
thread.start(); |
} |
} |
A.prints: MyThread: start() foll | owed by MyRunnable: run() |
B.prints: MyThread: | run() followed by MyRunnable | : start() |
C.prints: MyThread: start() foll | owed by MyRunnable: start() |
D.prints: MyThread: run() follow | ed by MyRunnable: run() |
E.compile time error |
F.None of the above |
ANSWER:A |
13: |
Multiple objects of multiple Threads to crea use the following code? | MyClass (given below) are us te new integer count. What w D | ed in a program that uses ill happen when other threads |
class MyClass{ |
static private int myCount = 0; |
int yourNumber; |
private static synchronized int | nextCount(){ |
return ++myCount; //myCount为static |
} |
public void getYourNumber(){ |
yourNumber = nextCount(); |
} |
} |
A. the code ill give ompilation error |
B. the code ill give runtime error |
C. each thread will get a unique number |
D. the uniqueness of the number | different Threads can’t be guaranteed. |
C |
14: |
What is the effect o | f issuing a wait() method on | an object |
Mutiple: |
1) If a notify() method has alre effect | ady been sent to that object then it has no |
2) The object issuin a notify() or notifyAll( | g the call to wait() will ha ) method | lt until another object sends |
3) An exception will be raised |
4) The object issuing the call t with any other objects using the rec | o wait() will be automatically synchronized eiving object. |