This week's giveaway is in the Testing forum. We're giving away four copies of TDD for a Shopping Website LiveProject and have Steven Solomon on-line! See this thread for details.
}
}
The books say that once the thread start() method or run() method completes or is under execution the second call to the run() or start() results in IllegalThreadStateException which should the result in the above case as well . However this code compiles and runs and produces the output:-"runnningrunningrunning"
of course not start() method start's a new process/thread, where by calling run() you'll simply running your process in same thread just like any ordinary java method.
It is true that start() internally calls run(), but run() and start() are not exactly the same.
Probably inside your run() method, you can try printing the name of the current thread executing it. Then, you might find what happens internally when you call t.run() and t.start().
To check whether your book is correct, you can try adding another t.start() to your code and see if you get an exception.
Minhaj Mehmood wrote:of course not start() method start's a new process/thread, where by calling run() you'll simply running your process in same thread just like any ordinary java method.
yes,by calling run() it doesn't start a new thread it simply run your process in currently running thread or same thread i.e. main in your case.
you can call run() again and again but you can invoke start() only and only once.
so that means my code should produce the output "running" for the one call of run() after the start() is invoked . But the output is "runningrunningrunning"
ragi singh wrote:so that means my code should produce the output "running" for the one call of run() after the start() is invoked . But the output is "runningrunningrunning"
ragi,thread is running three times.
two times when you are calling run method ,then the currently executing thread is running which is Thread3.
and one time when you invoke start().