| Author |
Regarding the IllegalThreadStateException
|
ragi singh
Ranch Hand
Joined: Mar 10, 2010
Posts: 198
|
|
Hi ,
I have a code sample :-
public class Thread3 implements Runnable{
public void run(){
System.out.println("running");
}
public static void main(String[] args){
Thread t=new Thread(new Thread3());
t.run();
t.run();
t.start();
}
}
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"
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
yes, because by calling t.run() you're not starting up the thread, it's just simply call run() method.
The thread can be run only by calling start() method of it.
HTH
Minhaj
|
SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
|
 |
ragi singh
Ranch Hand
Joined: Mar 10, 2010
Posts: 198
|
|
|
But run() and start() serve the same purpose ?
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
|
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.
|
 |
Vinoth Kumar Kannan
Ranch Hand
Joined: Aug 19, 2009
Posts: 276
|
|
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.
|
OCPJP 6
|
 |
Arjun Srivastava
Ranch Hand
Joined: Jun 23, 2010
Posts: 431
|
|
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.
|
SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
|
 |
ragi singh
Ranch Hand
Joined: Mar 10, 2010
Posts: 198
|
|
|
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"
|
 |
Minhaj Mehmood
Ranch Hand
Joined: Jan 22, 2007
Posts: 400
|
|
why the output should be "running" only? can you elaborate your code execution steps little more? what do you think in what manner your code execute?
Hint:You have called run() method 2 times.
|
 |
Arjun Srivastava
Ranch Hand
Joined: Jun 23, 2010
Posts: 431
|
|
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().
|
 |
 |
|
|
subject: Regarding the IllegalThreadStateException
|
|
|