Which of the following is always a result of attempting to compile and run the program? a. The program compiles and runs without error. b. The second attempt to start thread t1 is successful. c. The second attempt to start thread t1 is ignored. d. Compiler error at marker 1. e. An IllegalThreadStateException is thrown at run-time. f. None of the above e is correct. Here is the explaination:
The exam might have a question that invokes the start method on the same thread twice. The correct answer will state that an exception is thrown at run-time. In reality, the JVM would probably ignore the second attempt to start the thread and would probably not throw the exception if the thread were already dead. This code example includes a lot of thread synchronization code that ensures that the thread is not dead when the start method is invoked the second time. You can run this example yourself and get the correct result. On the real exam, the synchronization code will not be included.
As it said, on the real exam, the synchronization code will not be included. So, the first thread will probably be already dead before starting the second one. Which one is correct choice? If add an option like this: "G: The result is undetemined." It's better one,right?? Thanks, Roger
hi: "In reality, the JVM would probably ignore the second attempt to start the thread and would probably not throw the exception if the thread were already dead. " I don't know how accurate this statement is. The API says "throws IllegalThreadSateException if the thread was already started." It doesn't mention anything about ignoring the Exception. Also, I don't know where in the JLS does it say the JVM will ignore the second call to <start()> any pointers??
SCJD 1.4<br />SCJP 1.4<br />-----------------------------------<br />"With regard to excellence, it is not enough to know, but we must try to have and use it.<br />" Aristotle
For the purposes of the exam, invoking the start method on a thread that has already been started will generate an IllegalThreadStateException. If the start method is invoked a second time when the thread is already running then an illegal thread state exception 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.
Hi Roger It is because of a bug. Actually if the Thread.start() method is invoked on a same instance more than once an exception should be reported. So the fact that the JVM ignores the second attempt is actually the fault of the JVM. So the correct answer is e. An IllegalThreadStateException is thrown at run-time. Link to the bug report.
Joined: Aug 05, 2003
More clear for me. 1)
For the purposes of the exam, the exception is always thrown in response to the second invocation of the start method.
2)The fact that the JVM ignores the second attempt is actually the fault of the JVM. It is a bug! Thanks a lot!!! [ September 04, 2003: Message edited by: Roger Zhao ]