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();
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
The answer to this question is
e with the following explanation:
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.
To be honest, I find the answer and explanation unsettling. How do we know that this question is about our knowledge of the specification of a certain method, and not the actual behaviour of the JVM?
Supposing you are presented with a code just like below and asked, "What wil be the output of the program?"
Although 99% of the time A will finish first, there is no guarantee. So the answer could either be "AB" or "BA".
So how is this question different from Dan's?