1:) R0 z6 f4 q# r7 \ I: b
Which two CANNOT directly cause a thread to stop executing? (Choose Two)
: K* v& J! ?8 R& L: s" j8 w A.Existing from a synchronized block) H' c* S9 G$ C! t$ C
B.Calling the wait method on an object
! |; E) v" `: p! G C.Calling notify method on an object
" I6 r+ ]8 H% G& ?7 f D.Calling read method on an InputStream object5 b( ^: E# E7 k5 A5 m
E.Calling the SetPriority method on a Thread object. y) e) S( f; H, k& ?
Answer:AC。同55题
. _2 Q4 h9 x% |) `9 V+ l 2:
* }5 N1 P: V. ]% Z: W* s- x public class SyncTest{% G" E, I% P3 N$ Z8 P7 A8 q
public static void main(String[] args) {5 I4 G2 U: N' |2 n1 s/ l, L
final StringBuffer s1= new StringBuffer();. u1 f% q6 J- v8 j$ ` {- e% j/ k) c4 N
final StringBuffer s2= new StringBuffer();9 v: v4 F+ @$ x2 z. l- L2 h& G
new Thread () {
1 e6 b$ X- {2 E# R public void run() {" C2 k: ?3 u6 s' G( \0 a5 N
synchronized(s1) {0 f- r2 h& z$ Z3 y
s2.append(“A”);
) [- D2 I# W4 H) m# `4 k, G synchronized(s2) {
# \! q7 w9 U; o5 M; | s2.append(“B”);
: m9 P6 h; y' W% m, v, X a System.out.print(s1);
& R! G' A7 a1 N2 ?% I, g System.out.print(s2);
% p: C% G1 T+ X } d+ W/ q" r V5 L! X" Z
}/ v0 _) D a" A
} Z/ {8 |5 W! H- n( i+ h$ ]
}.start();" `$ b; I i7 T% I$ }# k2 Q
new Thread() {
. X! t$ F p2 n3 c. H- m, ^2 A$ q public void run() {9 a M, y- ]/ C! o: g9 m9 }6 Z4 `
synchronized(s2) {. R" D9 d+ Q7 g$ y5 c
s2.append(“C”);
1 l& M" _ e- m `# M3 D% E synchronized(s1) {! h& U: U" B( [+ R" S
s1.append(“D”);. Y7 e% E: k, h2 f' k% v+ F
System.out.print(s2);( l! k2 f+ [* X6 u0 u' o! D
System.out.print(s1);9 q. Z1 f0 ]' Y- m: e0 H
}( C2 S2 `; e ~/ d3 d% l1 j
}- F7 r0 {1 S e! { f* @
}
7 B0 O. |/ | @% B. W- B }.start();% ~/ H! w% {$ k
}: H6 `/ k9 T0 [6 c4 Z
}
! T% ^: F+ T& o9 l e+ M Which two statements are true? (Choose Two)* M; C9 c& E* e0 |
A. The program prints “ABBCAD”; d& f" I4 c9 E0 ^' Q) ]/ w
B. The program prints “CDDACB”
2 y$ w' {( T6 { C. The program prints “ADCBADBC”5 S5 d2 t& O8 }# Q3 m3 k
D. The output is a non-deterministic point because of a possible deadlock condition" }. B& o/ N/ b& k% @
E. The output is dependent on the threading model of the system the program is running on.( N8 B8 Y% ~' M+ H: N# `6 u7 v
Answer:DE1 L9 n* b* w. J( @
3:% I% _4 g0 r4 ^/ h
What will happen when you attempt to compile and run the following code?
; y0 u4 ]0 [" _9 p$ n public class Test{
- s s$ } L( |7 v# @ int i = 0;
! z+ b1 ~9 r5 G5 n/ g: g' V0 k public static void main(String argv[]) {2 l5 N1 Y/ @4 R$ `2 Q0 C9 u
Test t = new Test();# }1 U6 h# L$ Y9 O- @8 s K J
t.myMethod();
- [) p: x; p$ Z; z3 {6 | }
3 m A3 B/ s$ p6 d k public void myMethod(){
y$ @2 N. h* q4 { while(true) {
; [6 j; Z3 }" k7 g b) [; u8 v try { wait();6 _ h0 V! U3 H0 [3 ?& i
}catch (InterruptedException e) {}8 T, G) ^, }, t+ ?, B- H
i++;" `7 t: L& w8 l S- B
}
4 k% e' c( l5 g& K, X( P( k* ~ }
' h, I( J6 |) x2 R, w6 O }
. K& E" y H; n! G0 O; d4 H' S A. Compile time error, no matching notify within the method.
X9 Q$ G+ I( ?4 H' J B. Compile and run but an infinite looping of the while method.2 {; |# s0 d: y& ]2 ]0 j0 \
C. Compilation and run without any output.) j. O& h5 C: W4 w* l, n% r
E. Runtime Exception "IllegalMonitorStatException".% }2 l) H* o6 {$ _- [' V4 e
Answer: E( ^) \1 h" ?3 O- F) z* e6 M& M
Note: The wait/notify protocol can only be used within code that is synchronized. In this case calling code does not have a lock on the object(not synchronized) and will thus cause an Exception at runtime.$ e) [+ d. @+ A5 A# i$ b
4:/ k0 O6 O+ ~" n; y, y
1.10 What is the result of compiling and executing the following code?
0 {/ x) s. s( D" R, A public class ThreadTest extends Thread {
" R+ o% F* W m O! \6 n public void run() {- K) G) g' D! ]$ r0 f
System.out.println("In run");
' Q7 l' b4 `! R4 G8 G* [+ [* F% H yield(); System.out.println("Leaving run");0 N1 \, h! v7 E. K+ d$ @
}
2 D' _+ q+ T- ] public static void main(String args []) {2 c6 O+ N. m2 @9 v8 c: Y
(new ThreadTest()).start();% N; e* M: @: b5 z! v
}
/ W; Y9 c x8 K0 y0 p- g- X: N, Q7 [ }
2 B5 \5 U2 n2 X" F4 C6 H) O A. The code fails to compile in the main() method.
7 J* g$ e4 f5 q* ?2 Y7 S B. The code fails to compile in the run() method.
a* \6 ~0 k& M% M+ {1 S C. Only the text "In run" will be displayed.
$ X. L q! X7 s" _& r+ K D. The text "In run" followed by "Leaving run" will be displayed. b6 Y) i2 k0 W3 C* @
E. The code compiles correctly, but nothing is displayed.0 W, m8 p3 j* x( J. _% y0 ?; }
Answer: D
, N) z" p- d' r. M3 D* y7 c S/ G! H 5:
5 `; l J! _* M u- y) m 1.12 Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE6 Q+ a, F% V# v' o+ f# C
6:% W6 D2 J9 Y/ o0 M
1.12 Which of the following will definitely stop a thread from executing?5 Y: m5 v o5 F* E2 s5 d. n6 R' U
A. wait()
* S; M8 Z: E% }; c5 S1 @" }* T B. notify()- ?3 l* _9 e6 B3 ?( s
C. yield()
( z4 i7 L! H+ ?! N D. suspend()" c# `5 V+ t- D, B! {3 `
E. sleep()
2 _$ U# V* E5 t$ {$ L Answer: ACDE
6 x. t& [5 r+ g' W4 a& `2 _: ^. v 7:( T, X2 }$ P6 z
Which of the following statements about threading are true?
8 F! K- x9 n0 s$ K" a7 q8 A A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.
y$ P9 R. e8 v5 [2 \7 D B. You can obtain a mutually exclusive lock on any object.
, r. |' g# L1 t: W* U) d4 D9 V C. You can't obtain a mutually exclusive lock on any object.$ ^& k g$ D6 Z8 N8 D) z( y
D. Thread scheduling algorithms are platform dependent.% L5 |! n% I( K* Q- k
Answer: BD |