my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Thread Question on Exam Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Thread Question on Exam" Watch "Thread Question on Exam" New topic
Author

Thread Question on Exam

Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
Hi,all:

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


"There is a will,there is a way!"<br />SCJP1.4
Hanna Habashy
Ranch Hand

Joined: Aug 20, 2003
Posts: 532
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
Andres Gonzalez
Ranch Hand

Joined: Nov 27, 2001
Posts: 1561

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.

question 16
http://www.danchisholm.net/apr18/topic/section7/threads2.html


I'm not going to be a Rock Star. I'm going to be a LEGEND! --Freddie Mercury
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
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.
Roger Zhao
Ranch Hand

Joined: Aug 05, 2003
Posts: 73
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 ]
 
Don't get me started about those stupid light bulbs.
 
subject: Thread Question on Exam
 
Similar Threads
Threads
dan exam doubt 21
Question from Dan's Thread Exam
why this programs hangs ??
compile time errors in the program