This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question about Dan Watch "Question about Dan New topic
Author

Question about Dan's Exam (Thread)

Emiko Hori
Greenhorn

Joined: Nov 06, 2002
Posts: 6
Hi,
I got a question about Thread #4 Question 9.
==========================================
class A extends Thread {
public void run() {
System.out.print("A");
}
}
class B {
public static void main (String[] args) {
A a = new A();
a.start();
try {
a.join();
} catch (InterruptedException ie) {
ie.printStackTrace();
}
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.
Emiko
Greg Windwall
Greenhorn

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() {
System.out.println("A");
}
public static void main(String[] args) {
Thread t1 = new Thread(new A());
t1.start();
t1.stop();
t1.start();
System.out.println("main");
}
}
[ December 17, 2002: Message edited by: Greg Windwall ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about Dan's Exam (Thread)
 
Similar Threads
Threads
Thread Question on Exam
dan exam doubt 21
Question from Dan's Thread Exam
Dan's mock exam - Chap 9, Exam 2, Q18