a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 90|回复: 1

[考试试题] SCJP模拟试题分析之线程

[复制链接]
发表于 2012-8-4 12:33:16 | 显示全部楼层 |阅读模式
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
回复

使用道具 举报

 楼主| 发表于 2012-8-4 12:33:17 | 显示全部楼层

SCJP模拟试题分析之线程

  8:
6 u1 k$ d- l- d  x; p" l  Consider the following statement:1 t5 K! b/ I4 t$ t
  Thread myThread = new Thread();
. R% u! D! S& L  b! E" y4 h  Which of the following statements are true regarding myThread?
+ |# x9 g0 {: j9 m. g  A. The thread myThread is now in a runnable state.. e5 i$ @8 z/ ?4 N
  B. The thread myThread has a priority of 5.
; O& z; h7 m2 r% ]2 ]% a  H  C. On calling the start() method on myThread, the run method in the Thread class will be executed.
5 r3 S# K5 D( A% s% T+ v# F3 I% ]  D. On calling the start() method on myThread, the run method in the calling class will be executed.
' E; F* ~3 i; Y' S  Answer: C; R. e" d2 `2 n
  Note: the priority of myThread will be inherited from the Thread that called the constructor.
. Q+ d! K; [! r) V  9:1 F6 M$ U  V' Y$ Z4 n7 z/ T1 ^; |9 r, m
  What is the effect of issuing a wait() method on an object?(Mutiple)
9 J7 |; y5 ^; |4 y& h  N/ j$ a  1) If a notify() method has already been sent to that object then it has no effect6 _! Z$ c3 A' l
  2) The object issuing the call to wait() will halt until another object sends a notify() or notifyAll() method
; X5 n9 J+ t# x; y; r) i  w  3) An exception will be raised
8 Z! h+ \) P; B1 n; ~. O  4) The object issuing the call to wait() will be automatically synchronized with any other objects using the receiving object.2 [* G* V1 k, a6 b
  ANSWER 1)
7 }7 ~/ Q- ^6 n  ]  j  d; l  10:/ `+ _2 N+ N0 g' a  B* m  h/ l
  Pick all the true statements below./ s) g) L% G! O) C
  1) If a thread wants to call wait() on an object, the thread must own that object's lock.# [! x; ]4 |, L/ \8 h. [% m  K
  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.& L" \3 v" Y7 _7 ]1 u
  3) At the moment when a thread is notified, it automatically gets the lock of the object for which it was waiting.6 S( B( ~; L0 A/ E' R
  ANSWER 1
: k; @; Q9 P5 p# A- X" S( X  11:: x, c) m  G, l, \! y
  1. class Z {
9 ]8 D4 f+ {8 f  2.  public static void main(String[] args) {
( M/ u% S; n8 B/ T$ Z- B  3.   new Z();; x5 G( o# M  [: `# `
  4.  }6 c1 q% Y: z4 ]5 C& ~7 K
  5.% X# A+ N) m4 ^+ N7 b
  6.  Z() {
( b. C) `( f/ A, L6 E& J  7.   Z alias1 = this;. p# C6 d: q8 |$ D2 @9 g/ n) V8 |
  8.   Z alias2 = this;
+ a. g' e$ X3 e2 q  9.   synchronized(alias1) {5 W" R; j, Q- P8 [. |9 _7 |
  10.   try {
6 m; [4 A# ~0 W* B, I$ l  11.   alias2.wait();! k5 @- R1 Y" J& s8 _4 R
  12.   System.out.println("DONE WAITING");$ B- o" S& M3 s. |1 p6 j4 l
  13.   }" Z/ a$ N  q8 q4 B2 p3 d1 Q! O
  14.   catch (InterruptedException e) {0 q5 P% A" X- F6 c( k% q
  15.   System.out.println("INTERRUPTED");
0 w8 |. @. G( M1 s% _5 \" U  16.   }
6 x, Y& \1 d8 K7 d/ u6 F. O  17.   catch (Exception e) {
" Q" O' Z9 A; Z% ^/ D& `! P  18.   System.out.println("OTHER EXCEPTION");% }# v/ N4 A; T5 C2 d# u
  19.   }7 G% h" i7 J. I/ n
  20.   finally {
8 y5 g5 ?3 T) c% Q& Q  21.   System.out.println("FINALLY");
7 K- V4 Y: o/ D( I+ z0 l  22.   }
# v3 A1 u9 S: m1 v  23.   }
3 i; T# s8 w# u  24.   System.out.println("ALL DONE");
! @# c8 c, s% N2 S7 Y5 Q  25.  }
5 E7 N- t  i3 k2 _. s( z" E$ a  26. }
# I2 f3 ]: U! k' f  C% g  Mutiple:$ d" G9 Y) N' u% s
  1) Compiler error.+ o; r' O$ n5 R
  2) The application compiles but doesn't print anything.& \$ Y. b9 i( F
  3) The application prints "DONE WAITING"./ i/ G) g% s) S  `
  4) The application prints "INTERRUPTED".
' L) n/ z% z& e" G" p" o" L0 N  5) The application prints "OTHER EXCEPTION".
5 f4 @$ n+ }/ X" a8 p6 j  ANS: 2)
- I, f) r/ n- Z. E4 N  12:
$ P; k7 Y: n: p$ I3 r! I4 d9 ]  What will happen when you attempt to compile and run the following code?+ d% V  I- _; V1 R
  class MyThread extends Thread{
5 z+ |: a4 n4 {7 i  public void run(){% M, x% B+ P0 M: `) K
  System.out.println("MyThread: run()");
; Y/ ~% [! }/ V& n( S, [" {  }/ O9 j' q$ t! H! o- N8 R5 s
  public void start(){////注重,此体例重写后,不再有CALL RUN()体例的功能。
1 W: _" g8 h( X  Z, E! f6 T  System.out.println("MyThread: start()");
/ v1 C1 _5 M+ J) ], G  }
9 t3 c! y7 B" Q4 }/ H' F  }# C  x6 U' j. l
  class MyRunnable implements Runnable{
; K  o% @; i# d2 k, s+ E  public void run(){" _* |/ B- f$ Z  A" l1 m
  System.out.println("MyRunnable: run()");; I! ]* {4 p; Z- A) q8 q: _2 I
  }
8 J* I2 p0 I8 p9 ~6 I( ^! Z( y! e8 L  public void start(){//Runnable 并无此体例,所以,除非本类的对象可以CALL 它,否则,将CALL THREAD 的start()执行run(): X( v5 M& k2 k& Z4 c) y
  System.out.println("MyRunnable: start()");
* d5 w" Q! v, x( q% H) W. r, d, \  }
# ^/ ^9 E; T6 \7 K9 [% \6 n. f  }7 w/ I+ ~5 J: A& L
  public class MyTest {, \; H, g& Z! p: H
  public static void main(String args[]){; T5 G& Y; ^% Q( ^  m. n' P4 p1 G
  MyThread myThread = new MyThread();
; n7 \. N+ i# Y& I; t  MyRunnable myRunnable = new MyRunnable();
3 Y" B5 t6 G1 G# j+ y  Thread thread = new Thread(myRunnable);
* u# o) v( E( i7 T1 p6 @8 u  myThread.start();
0 z1 Y7 F! D2 d6 r. |# v; v$ b* [  thread.start();1 @9 ~  t3 Z% j
  }2 y* z$ G' A$ q8 v7 K4 T
  }
0 K0 |+ [/ w  ?% j" f  A.prints: MyThread: start() followed by MyRunnable: run()' i& {* R& R: f. X/ R6 s1 B
  B.prints: MyThread: run() followed by MyRunnable: start(); _& b. T3 v8 u2 i
  C.prints: MyThread: start() follow</p>
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Woexam.Com ( 湘ICP备18023104号 )

GMT+8, 2024-6-18 11:17 , Processed in 0.494533 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表