This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
anbarasu aladiyan wrote:
You can call start() more than one time, if the thread is alive or NOT dead.
You will get exception when you try to invoke a thread that already dead.
Actually no. A thread can only be started once. Period.
As for why, it is simply defined that way. I guess they could have done something like... ignore start() requests if it is already started or reinitialized everything, if the previous run has completed, but why? Why complicate matters? A thread is created, a thread is executed, and the thread object can be thrown away afterwards.
Hi, anbarasu aladiyan.... Compile and run your coding......., You will get a RuntimeException, java.lang.IllegalThreadStateException... Because, the compiler only check the syntax of your coding. Logic of your coding can't be understand by the compiler. ... So It allows multiple invocation of start() method on a Thread. But, at runtime, the JVM check the logic of your coding, that's why it flags a runtime exception.... Hope you understand my last post reply.......
A call to start method keeps that thread in queue for the scheduler to pick and run.
Now for the below code
In the first call the thread will be kept in the queue, now for the second call either main thread has to wait till the same existing thread object completes it's execution (as we can't have both in the queue as the object is same) or throw an exception.
They chose the second option.
This is just my analysis. There might be some other reasons also.