File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question about Dan's Exam (Thread) 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 "Question about Dan Watch "Question about Dan New topic

Question about Dan's Exam (Thread)

Emiko Hori

Joined: Nov 06, 2002
Posts: 6
I got a question about Thread #4 Question 9.
class A extends Thread {
public void run() {
class B {
public static void main (String[] args) {
A a = new A();
try {
} catch (InterruptedException ie) {
a.start(); // 1
What is the 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

Dan's answer for this output was

e. "An IllegalThreadStateException is thrown at run-time."
Dan's explanation told us that we should know the actual behavior of 1.4 version of the JVM.
If in the reality, the original thread has been dead by a.join() and line 1 has been ignored, I thought that this would be the actual behavior of the JVM (I tried it and it didn't throw any exception -- compiled fine and printed "A").
Yes, I am aware of the fact that if there is no join() method, IllegalThreadStateException will be thrown.
Is there any way to theorize that the answer "can" be "a" and possibly "c"? I'm curious. Thank you very much for your help.
Greg Windwall

Joined: Dec 12, 2002
Posts: 12
I don't think that the removal of a.join() would help anything. You can't run a single instance of a thread multiple times, which is what is being done here with two a.start() lines. You get the same error with:
class A implements Runnable {
public void run() {
public static void main(String[] args) {
Thread t1 = new Thread(new A());
[ December 17, 2002: Message edited by: Greg Windwall ]
I agree. Here's the link:
subject: Question about Dan's Exam (Thread)
It's not a secret anymore!