• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Question about Dan's Exam (Thread)

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
    Bookmark Topic Watch Topic
  • New Topic