200812191512Chapter 9: Threads(一)_解答

<thead> No. Answer Remark </thead>
Answers: Certified Java Programmer Mock Exam
1 b  c  h  notify  notifyAll  wait   
2 e  g  sleep  yield   
3 d  h  i  resume  stop  suspend  For the purposes of the exam, you don't need to memorize the deprecated methods of the Thread class. Even though a question such as this will not be on the exam, every Java programmer should know that the deprecated methods should not be used in new programs.  
4 a  e  h  join  sleep  wait   
5 a  e  h  join  sleep  wait   
6 b  c  f  notify  notifyAll  wait   
7 InterruptedException  The methods Object.wait , Thread.join and Thread.sleep name InterruptedException in their throws clauses.  
8 b  c  Prints: 0  Prints: 1  The new thread is started before the print statement, but there is no guarantee that the new thread will run before the print statement is processed. The guarantee could be provided by placing the method invocation expression a.join() before the print statement, but the invocation of the join method does not appear in the program. If the new thread runs before the print statement is processed, then 1 is printed. Otherwise, 0 is printed.  
9 A member variable that is an object reference  Primitives don't have locks; therefore, they can not be used to synchronize threads. A method local variable that is a reference to an instance that is created within the method should not be used to synchronize threads, because each thread has its own instance of the object and lock. Synchronization on an instance that is created locally makes about as much sense as placing on your doorstep a box full of keys to the door. Each person that comes to your door would have their own copy of the key; so the lock would provide no security.  
10 b  e  f  g  h  i  A compile-time error occurs if the expression produces a value of any primitive type  If execution of the block completes normally, then the lock is released  If execution of the block completes abruptly, then the lock is released  A thread can hold more than one lock at a time  Synchronized statements can be nested  Synchronized statements with identical expressions can be nested   
11 The process of executing a synchronized method requires the thread to acquire a lock  The synchronized modifier can not be applied to a class. A method that overrides a synchronized method does not have to be synchronized. If a thread invokes a synchronized instance method on an instance of class A, then the thread must acquire the lock of that instance of class A. The same is not true for synchronized static methods. A synchronized static method is synchronized on the lock for the Class object that represents the class for which the method is a member.  
12 c  d  e  f  The Ready state to the Running state  The Running state to the Not-Runnable state  The Running state to the Ready state  The Not-Runnable state to the Ready state  A dead thread can not be restarted.  
13 b  c  d  f  The Thread.yield method might cause the thread to move to the Ready state  The same thread might continue to run after calling the Thread.yield method  The Thread.yield method is a static method  The Thread.sleep method causes the thread to move to the Not-Runnable state  The Thread.yield method is intended to cause the currently executing thread to move from the Running state to the Ready state and offer the thread scheduler an opportunity to allow a different thread to execute based on the discretion of the thread scheduler. The thread scheduler may select the same thread to run immediately, or it may allow a different thread to run. The Thread.yield method is a native method; so the behavior is not guaranteed to be the same on every platform. However, at least some implementations of the yield method will not yield to a thread that has a lower priority.  
14 Thread.yield method  The Thread.yield method may cause a thread to move into the Ready state, but that state transition is not guaranteed. The JLS states that the Thread.yield method provides a hint to the thread scheduler, but the scheduler is free to interpret--or ignore--the hint as it sees fit. Nothing in the JLS suggests that the thread might move to the Not-Runnable state.  
15 None of the above  A dead thread can not be restarted.  
16 Ready   
17 b  d  e  h  The Thread.start method causes a new thread to get ready to run at the discretion of the thread scheduler  The Runnable interface declares the run method  The Thread class implements the Runnable interface  Some implementations of the Thread.yield method will not yield to a thread of lower priority  The Object.notify method can only be called by the thread that holds the lock of the object on which the method is invoked. Suppose that thread T1 enters a block that is synchronized on an object, A. Within the block, thread T1 holds the lock of A. Even if thread T1 calls the notify method immediately after entering the synchronized block, no other thread can grab the lock of object A until T1 leaves the synchronized block. For that reason, the transfer of control from thread T1 to any waiting thread can not be accelerated by moving the notify method to an earlier point in the synchronized block. The behavior of Thread.yield is platform specific. However, at least some implementations of the yield method will not yield to a thread that has a lower priority. Invoking the Thread.yield method is like offering a suggestion to the JVM to allow another thread to run. The response to the suggestion is platform specific.  
18 a  c  f  Thread.MAX_PRIORITY = 10  Thread.NORM_PRIORITY = 5  Thread.MIN_PRIORITY = 1   
19 b  d  A program will terminate only when all user threads stop running  A thread inherits its daemon status from the thread that created it   
20 Prints: A  If a Runnable target object is passed to the constructor of the Thread class, then the Thread.run method will invoke the run method of the Runnable target. In this case, the Thread.run method is overridden by A.run . The A.run method does nothing more than print the letter A. The invocation of the A.start method inside the main method results in the invocation of A.run , and the letter A is printed. The B.run method is never invoked.  
21 Prints: T1T1T3  The Thread.currentThread method returns a reference to the currently executing thread. When the run method is invoked directly it does not start a new thread; so T1 is printed twice.  
22 Compile-time error  The Runnable.run method does not have a throws clause; so any implementation of run can not throw a checked exception.  
23 An IllegalThreadStateException is thrown at run-time  For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. The actual behavior of Java might be different. If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored, and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4 version of the JVM.  
24 An IllegalThreadStateException is thrown at run-time  For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. The actual behavior of Java might be different. If the start method is invoked on a thread that is already running, then an IllegalThreadStateException will probably be thrown. However, if the thread is already dead then the second attempt to start the thread will probably be ignored, and no exception will be thrown. However, for the purposes of the exam, the exception is always thrown in response to the second invocation of the start method. This is a case where the exam tests your knowledge of the specification of the Thread.start method and ignores the actual behavior of the 1.4 version of the JVM. The Thread.join method is included here to verify that the thread is already dead before the start method is invoked the second time. If this code is executed using the 1.4 version of the JVM, the exception will not be thrown. However, for the purposes of the exam, the exception is always thrown. The real exam question will probably not include the invocation of the join method.  
25 b  d  Prints: [T1,A][T2,B]  Prints: [T2,B][T1,A]  Since method m1 is synchronized, it is guaranteed that no more than one thread will execute the method at any one time. Even though the start method is invoked on thread T1 first, there is no guarantee that it will actually begin to run first.  

Copyright © 2002-2003, Dan Chisholm
沒有上一則|日誌首頁|沒有下一則
回應
關鍵字
    沒有新回應!





Powered by Xuite