8:
0 M, t* x- N# g Consider the following statement:
* u- a* K/ u o Thread myThread = new Thread();
* ~; ~+ l, f$ u5 V7 T- R Which of the following statements are true regarding myThread?( [+ L/ w( T0 Q* t' `
A. The thread myThread is now in a runnable state.
7 `# @" g, L- K2 z. D B. The thread myThread has a priority of 5.. t7 E/ E6 o# `
C. On calling the start() method on myThread, the run method in the Thread class will be executed.
9 r+ S9 w( z: N D. On calling the start() method on myThread, the run method in the calling class will be executed.
0 [% V6 U, L* s% t$ p+ t1 e" [% R Answer: C
( @! {* n. G* \! N- ~+ R; c Note: the priority of myThread will be inherited from the Thread that called the constructor.
: ]+ l4 b( a0 y- B" K4 w 9:4 U0 j3 K: u& M
What is the effect of issuing a wait() method on an object?(Mutiple)
2 s& e: m8 |( E6 k6 B$ g- T y! G 1) If a notify() method has already been sent to that object then it has no effect6 ^' O6 K8 O) ~( _' R8 g5 T
2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
% x4 V5 B1 S9 b 3) An exception will be raised
/ f ?' y: m( @) \9 s6 q ? 4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.3 A/ F8 p( x( D5 b# V9 T. c- H# I
ANSWER 1)
0 \8 V5 E: y5 f* o 10:6 ^" y0 Z% S+ e0 ]. N3 |
Pick all the true statements below.
" ?5 o# G1 |* M) l6 A8 @* s 1) If a thread wants to call wait() on an object, the thread must own that object's lock.
) }# b) k! D+ v' |, X 2) There is a method that you can call on an instance of the Thread class that puts the instance to sleep for a specified number of milliseconds.7 y' N1 \7 K2 L0 t2 Q
3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.
' n% t" b7 }' c$ [ ANSWER 17 s% t( u; W, ]/ y- ^3 Y- ]/ x
11:% Y! Y o4 _ w- Q- p
1. class Z {
1 y. X. @3 F, t Z& v7 j p 2. public static void main(String[] args) {
+ j7 |6 N9 G9 u; t# _ 3. new Z();- Q$ a; a+ C* O. B) H, Y
4. }
+ S- z! ~/ w4 k% o+ R5 K2 s 5.. Y; g! I6 |; r9 s& n8 ^$ T
6. Z() {, w$ T7 g8 l& v. C
7. Z alias1 = this;
& w$ F. Z5 [3 K- c, I9 O4 v 8. Z alias2 = this;0 T' z+ k. K# T7 u m" z: A
9. synchronized(alias1) {
, o; Y. \" f" E% I1 C4 _ 10. try {
0 y" l: D# S' }4 O' e- N+ H6 y* R) @ 11. alias2.wait();" O( Y; j; A) R8 ^/ Y
12. System.out.println("DONE WAITING");
2 T* f3 W; E7 M4 o8 B( t8 ]1 t 13. }
1 f0 N* R! z) i 14. catch (InterruptedException e) {
: E9 W- v5 R# e' L4 N$ j& c+ [6 J8 t 15. System.out.println("INTERRUPTED");
+ A) j# v [8 y! N& { V* l$ Q5 P) Y 16. }
, t2 L5 h( @; r 17. catch (Exception e) {
% ?& v$ `; @1 a$ s# v- T: I 18. System.out.println("OTHER EXCEPTION");
" m5 F( R( F7 k/ \* b0 J9 k 19. }
0 Q8 i6 O% h% `6 q 20. finally {
' F, C8 w6 W% s; s8 F( O6 ~ 21. System.out.println("FINALLY");
- y# i; U" q& q 22. }$ @& N- U0 d/ h
23. }1 m2 R9 M7 v& X: _. r
24. System.out.println("ALL DONE");
+ U u1 V7 b1 N' a 25. }; @6 r. _" i* f7 l! p. q' b1 T
26. }
7 p$ ]( [) u9 K1 N. ]. I Mutiple:4 y6 p+ ^# x' T$ E7 D: w
1) Compiler error.! f! n- r, k1 t7 Z
2) The application compiles but doesn't print anything.* o" T3 U9 P, B
3) The application prints "DONE WAITING".
( x( {+ I) H. c# y/ S 4) The application prints "INTERRUPTED".
b+ P0 F) W) G. V 5) The application prints "OTHER EXCEPTION".
1 g, L& e3 F9 ]( Q1 `, g ANS: 2)
& H( x6 N2 y3 W1 ~( X" c I 12:
0 i+ a% W9 `! l4 _ What will happen when you attempt to compile and run the following code?
- H& q7 ?9 d& {5 ]. ~" K' i class MyThread extends Thread{
- s' }. ^# Y% ^8 r8 o0 E. }2 X' F public void run(){
& s9 `; ?1 ^+ [5 O- @3 k: F System.out.println("MyThread: run()");
0 i# W" b$ |; U) b }
' R5 ~% l9 T: w* F5 Y public void start(){////注重,此体例重写后,不再有CALL RUN()体例的功能。6 F7 k1 S, \- ?) F: I
System.out.println("MyThread: start()");1 m7 F# S) t* [( D2 S. _5 U
}+ P7 R+ x. v: d5 E! y
}% G, Q0 Q: d" r* ]& T" u! Q
class MyRunnable implements Runnable{
# F9 C' a% ?5 m* d" c. h/ v public void run(){
, g( x k! f8 c" S8 G+ u; x System.out.println("MyRunnable: run()");
) K* l% }4 {( O: n2 J/ n( N }
/ H+ j8 u1 u a. a3 h% J0 K public void start(){//Runnable 并无此体例,所以,除非本类的对象可以CALL 它,否则,将CALL THREAD 的start()执行run()
2 G9 w; Q3 y& h, f. b System.out.println("MyRunnable: start()");3 N& l* v) k/ O5 N5 q
}
* r3 B+ ?2 I7 q1 d4 D }
* j. D! _& u1 O2 h public class MyTest {! x- R. I- {5 w, V: n/ d5 R
public static void main(String args[]){
5 E, \* O; F' P8 o( \8 ^ MyThread myThread = new MyThread();; l2 s3 F) O' P4 {( {% s, E
MyRunnable myRunnable = new MyRunnable();- X! ]) [9 R! @ G! Z
Thread thread = new Thread(myRunnable);
# M, L: q% ~( u; ^2 X myThread.start();( S2 S) H5 C' _* a
thread.start();
. G8 g& D& C2 N% [9 n, Z G } T2 I' }* U! A3 H" k+ f
}
0 f, K8 A+ w5 F3 [. P' X* f, p A.prints: MyThread: start() followed by MyRunnable: run()! e7 H6 _2 ~8 `6 Y5 ]! o
B.prints: MyThread: run() followed by MyRunnable: start()3 u% [5 V4 O0 h4 M5 ]- M8 k
C.prints: MyThread: start() follow</p> |