• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Regarding the IllegalThreadStateException

 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But run() and start() serve the same purpose ?
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 276
Chrome Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Srivastava
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
ragi singh
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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().
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic