Originally posted by sekhar variam:
But by calling a.join() aren't we ensuring that the thread is already dead before starting it...In that case shdn't the answer be a)
Thanks,
Sekhar..
The exam is always looking for the answer that matches the
Java Language Specification or the Javadoc on the class. When the JLS or the Javadoc disagree with the actual behavior, then the JLS or Javadoc is preferred over actual behavior.
In this case, the join method ensures the death of the thread before the second attempt to start the thread. The actual result is that the second attempt to start the thread is ignored without error. However, the Javadoc states that an IllegalThreadStateException is thrown if the thread was already started. For the purposes of the exam, just assume that a second attempt to start a thread will always result in an exception. The purpose of this question is to stress the point that we must assume an exception is thrown even though we know that it won't be--at least not on the platform that I'm running.
The emphasis on the specification over actual behavior is based on the fact that the actual behavior might someday become more consistent with the specification. Therefore, code should not be written based on an assumption that the actual behavior has become a defacto standard.
In this case, one could argue that the Javadoc is not clear and does not disallow the possibility that the exception is not thrown. On the other hand, one could also argue that the Javadoc does not guarantee that the exception won't be thrown. Based on the ambiguity we should just assume that we can not start a thread twice without risking problems.