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.
Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Regarding the IllegalThreadStateException

 
Ranch Hand
Posts: 198
Oracle Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But run() and start() serve the same purpose ?
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 276
Netbeans IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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().
 
I wasn't selected to go to mars. This tiny ad got in ahead of me:
Free, earth friendly heat - from the CodeRanch trailboss
https://www.kickstarter.com/projects/paulwheaton/free-heat
reply
    Bookmark Topic Watch Topic
  • New Topic