• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Thread States

 
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Bert's book states that a thread is done being a thread when its target run() method completes. And that the thread is then considered dead.

If that's the case, then how come you can invoke the run() method on a thread multiple times? That makes me think that the thread goes from dead (when the first invocation of run() completes) to runnable by simply invoking the run() method again.
 
Ranch Hand
Posts: 584
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Shawn,

The key point to understand here is that when you call the run() method directly from within your code, such call does not actually creates another thread, it runs from within the same thread where the call was made.

On the other hand, when you call start() method, a new thread is created and your run() method is implicitly called from within this new thread.

Did you get the point ?
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You do not start a thread by calling it's run method. You can only start a thread by calling its start method. The thread is then scheduled to run and when it is given time in the processor, run is called.

Calling run() directly does not create a new thread. It simply executes the lines of the run() method in whatever thread calls the method.
 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understand what you're saying but the statement above still contradicts the figure on p.691 of the book.

Either a thread must be dead and never return to the runnable state, or it can run and then it just goes back to runnable.
 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Shawn Kuenzler:
I understand what you're saying but the statement above still contradicts the figure on p.691 of the book.

Either a thread must be dead and never return to the runnable state, or it can run and then it just goes back to runnable.



But the thread is not returning the runnable state.

Consider this example.



Just because I call the run method of MyThread, that does not put the thread in the runnable state.

The method is executed by the main thread.
[ April 23, 2006: Message edited by: Keith Lynn ]
 
Shawn Kuenzler
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here was my thinking (hope you don't mind me hacking your example)

 
Keith Lynn
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well actually you are never putting the thread into the runnable state. Calling run() does not create a new thread. It simply runs the code in that method in the thread that calls it.

In order to put a thread into the runnable state, you must first call its start() method. This will allow the thread to be scheduled to run. Once the processor is able to run it, the thread enters the running state.

In the example, MyThread is a subclass of Thread, but even though it is instantiated, it is never started, so it never runs.
[ April 23, 2006: Message edited by: Keith Lynn ]
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic