a我考网

 找回密码
 立即注册

QQ登录

只需一步,快速开始

扫一扫,访问微社区

查看: 88|回复: 1

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

[复制链接]
发表于 2012-8-4 12:33:16 | 显示全部楼层 |阅读模式
1:
  |( v3 C4 j* }3 \  Which two CANNOT directly cause a thread to stop executing? (Choose Two)
+ a! @+ `# Y) w- x$ R  A.Existing from a synchronized block1 y' H- G# r+ t' ]6 K
  B.Calling the wait method on an object' {" F# k" O" N
  C.Calling notify method on an object
0 A8 h& k" Z: K7 k" W1 D  D.Calling read method on an InputStream object
0 E4 q4 B' s1 N$ b0 }. s  E.Calling the SetPriority method on a Thread object% x5 W9 j" r2 M# E, c
  Answer:AC。同55题* v' b. Z( X) Q9 o- O
  2:
: N8 Y7 z3 y! l" o  public class SyncTest{
. I% s: u+ C0 L2 g% a6 A0 |  public static void main(String[] args) {
/ a7 i8 j. y/ ]% y% j( w1 G, X' @: v  final StringBuffer s1= new StringBuffer();
" _" f! W/ v- _, \8 B  final StringBuffer s2= new StringBuffer();
: l! P, O( g4 }8 {2 v$ B; i( W+ ~  new Thread ()  {
: |7 ?- Z8 E! J9 u' X0 T' U" D! `  public void run() {( m0 d" Z0 X+ [1 B
  synchronized(s1) {
! c  a, t1 f# _& F& \% v  s2.append(“A”);
* x3 ?7 q) e& t" y' ~' B3 }  synchronized(s2) {
3 ~3 E, q0 d( B9 V  s2.append(“B”);
. c2 Q) u' i7 V6 G/ g6 v  System.out.print(s1);( n' [1 i  F' S# `: O& @
  System.out.print(s2);
. n; l) o& t2 z1 T( p1 o8 k+ [  }7 B- n# f+ y+ h; g7 V
  }
/ M, n' X# r  ?# \& @4 x2 a  }% t2 u2 X7 D" @) F3 E2 |$ E# }# @
  }.start();
* Z8 _+ c! w0 ^6 Z3 w$ s( Z: Z  new Thread() {
9 {& q6 T, @' r6 p  public void run() {) q* [2 `/ K, w! k' F7 c
  synchronized(s2) {0 O$ `! o9 K  {
  s2.append(“C”);
  c+ l3 x! @2 G+ e" o" f  synchronized(s1) {. L% |9 S, ]$ I
  s1.append(“D”);  Q; E* E2 V# Y$ O% s  a; m3 E
  System.out.print(s2);7 g- d9 m3 c8 `8 F8 |! p0 }
  System.out.print(s1);) v3 V3 D3 d5 Z1 ~
  }
' I, y: b7 L' n  }
; b6 ^" q: t# v  }
' r& J5 D& c$ C% `" n$ q' l  }.start();
7 j8 n5 U) a" J9 O% C- o$ ?2 t  }& Q2 V& Q6 m( b4 E' C
  }
$ ^1 U2 R* v9 U1 {/ o9 T" p8 Q  Which two statements are true? (Choose Two)
0 V& Q0 q1 E$ j8 P  A. The program prints “ABBCAD”  Y, H: o/ M. L: x
  B. The program prints “CDDACB”
8 e2 i- T# g$ k8 I+ \& ^, l' n. c  C. The program prints “ADCBADBC”) Z8 R5 x6 U+ d
  D. The output is a non-deterministic point because of a possible deadlock condition
) Z: c2 E! b9 K$ Q- R, a  E  S9 K  E. The output is dependent on the threading model of the system the program is running on.
/ d- G; N; E6 W  Answer:DE3 y0 F1 ~! a8 j
  3:
4 G" m' b& O% f( ]: u3 W1 @0 h  What will happen when you attempt to compile and run the following code?" J+ F" d/ b# x
  public class Test{
" z' q) r9 L3 W. _  int i = 0;2 d% x( ]2 O- [, _
  public static void main(String argv[]) {
6 ?  R7 s* @# @  Test t = new Test();0 H* Z+ u: B/ x9 K5 v7 ]1 f
  t.myMethod();7 s1 t# g" m% N5 S
  }7 H' T8 R% y( t5 ~
  public void myMethod(){
+ Q! y; _# H! u4 n) H8 {" i  while(true) {6 A1 k, }9 C2 f5 y! _6 i; A; {. ?
  try {  wait();8 O, J8 q/ p+ K2 K5 ~, X! J
  }catch (InterruptedException e) {}. m: f$ A$ N* a# b- _7 C) E
  i++;$ o6 W2 T/ ~* Z) q" b  l  M
  }. Q2 R0 M8 V5 S5 p" f2 `
  }
9 ^8 D1 O3 z$ ~1 x  }: y* y# B8 T: J7 K
  A. Compile time error, no matching notify within the method.
# Q3 Z# K0 @& k9 T% x$ S  X. d  B. Compile and run but an infinite looping of the while method.
$ `5 W' m9 H, b4 o4 K* \' m  C. Compilation and run without any output.
8 u5 c/ S' g0 q& ]- ^  E. Runtime Exception "IllegalMonitorStatException".# p6 r, l6 x; ~; n& \/ f" N6 h
  Answer: E5 T$ W6 a/ a3 K  v3 N( n6 i. 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.1 C1 B1 v: L- X; F
  4:) }/ \/ m) n: u; n8 X
  1.10 What is the result of compiling and executing the following code?8 M' N/ }  D1 y. }: k5 P6 D) t
  public class ThreadTest extends Thread {
/ }  X' i3 h0 k! g  public void run() {5 ^' ?7 l! e5 I/ g% V# r' O  Q
  System.out.println("In run");
/ m2 ^: e& L% J& L+ l% I1 s! k( X  yield();  System.out.println("Leaving run");4 e  r( ~  n# {  R4 b1 ^
  }
# q) J0 D4 @3 d, J7 V0 [  public static void main(String args []) {! ~+ q* H" l: ~% w5 a
  (new ThreadTest()).start();; o! F0 `7 \. ~3 f
  }, `1 z  K3 H& M! `1 f' @8 A
  }
+ T: }) N/ |* w9 T1 P4 p  A. The code fails to compile in the main() method.
7 H! {/ b8 C1 |' n2 J  B. The code fails to compile in the run() method.+ H) }; |7 U& W3 @/ U2 Z9 }
  C. Only the text "In run" will be displayed." r1 j) s; `; l
  D. The text "In run" followed by "Leaving run" will be displayed.* ?0 h5 w7 E. c4 S) e- c* a
  E. The code compiles correctly, but nothing is displayed.8 A: c5 ^3 V3 A" M0 b$ N& T
  Answer: D ! T0 ^& B% s4 g
  5:$ H0 V& [/ Q4 c# \* f) X; N; z0 Q; d
  1.12 Which of the following will definitely stop a thread from executing?A. wait()B. notify()C. yield()D. suspend()E. sleep()Answer: ACDE) t- }7 ~1 E: |
  6:" @" l+ ^/ ]+ E+ L7 h: ?  N
  1.12 Which of the following will definitely stop a thread from executing?
# v3 q+ s6 A5 f! G" b  A. wait()( B% [2 ~- W% W7 Q5 a4 \) }  c
  B. notify()
: l' M) ^& i, _  C. yield()- t/ ~  C: O0 ?! c9 P
  D. suspend()2 W" U( w2 H5 y. @" q- o6 z
  E. sleep(): i  V3 A* G6 O
  Answer: ACDE0 ~+ i+ l0 n, K( Y, e- G  N
  7:4 Q# L( O4 }6 y
  Which of the following statements about threading are true?! V+ H" \  r7 J, l  g& W* \
  A. You can only obtain a mutually exclusive lock on methods in a class that extends Thread or implements runnable.. |  u9 S. G/ K1 y5 W7 ]
  B. You can obtain a mutually exclusive lock on any object.
  I6 V% v1 {6 D% l" C) J. `  C. You can't obtain a mutually exclusive lock on any object./ M7 e; T9 t: i
  D. Thread scheduling algorithms are platform dependent.
! V+ x3 l+ u" D7 j6 e  Answer: BD
回复

使用道具 举报

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

SCJP模拟试题分析之线程

  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>
回复 支持 反对

使用道具 举报

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

本版积分规则

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

GMT+8, 2024-5-22 06:04 , Processed in 0.153657 second(s), 23 queries .

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

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