This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Threads

 
Kristof Camelbeke
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a question concerning the method start().... I have seen an example class which extends from Thread and overrides the run() method of Thread. But I have read that in order to start a thread you should invoke the start method on the instance of the subclass, is this correct ? Because in this example the start method is never invoked ; does this mean that the overriden run() is never executed ???
 
Kamlesh Sangani
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya, start() internally calls run(). u need not call run().
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13056
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NO - start does NOT "call run" - start puts the Thread in a runnable state so the JVM will schedule it to execute the run method. You have to call the start() method of the Thread or the class that extends Thread.
While you can have another Thread call the run method, that defeats the whole idea. The JavaDocs for the java.lang.Thread class are very clear on this - I heartily recommend that you read them.
Bill
 
Nick Way
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The other way is to have your class implement Runnable, then you can start a thread as follows:
new Thread(new YourClass()).start();
That will call the run() method in your class.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic