Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Thread states  RSS feed

 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's yet another diagram of the possible states of a <code>Thread</code> object. There seems to be no official version of the state names, so I felt free to invent my own. I'm posting this mainly to check my own knowledge; if anyone sees a problem then please comment.
<pre><code>
|----------|
/-\ | |
|*| ---<init>----->| New |
\-/ | |
|----------|
|
start()
|
\|/
|-----------------------| |-----------------------|
| | | |
| | | Contending |
| | |<--------got lock------| for lock |
| | | | |
| Ready | | |-----------------------|
| |<-----|<-sleep expired-----| /|\
| | | | |
| | | | |{blocked,
| | |<-interrupted--| | | needs lock}
|-----------------------| | | |
| /|\ /|\ | | ---------------------------
| | | | | /|\ /|\ /|\ /|\
| | | | | | | | |
| | | |----------| | | | |
| | | | | | | | |
got preempted yield() | Sleeping | | | | |
CPU | | | | | monitor | |
| | | |----------| | notified | |
| | | /|\ | | | |
| | | | | | wait |
| | | | | | expired |
\|/ | | | | | | |
|-----------------------| | | | | interrupted
| |---sleep()-------------| | | | |
| | | | | |
| | | | | |
| Running |---entered synchronized code-----| |--------------------|
| | | |
| | | |
| |---wait() on monitor------------------>| Waiting |
|-----------------------| | |
| | |
| |--------------------|
run()
exited
|
|
\|/
|----------|
/-\ | |
|X|<--garbage--| Dead |
\-/ collected | |
|----------|
</code></pre>
jply
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Excellent job!
At last we can answer immortal questions �how to stop a thread� and �which methods move a thread from X state to Y state� � by pointing to your diagram !
How about deprecated methods? They still can be used and people often ask about them�
I would add stop()(DEPRECATED) and System.exit() for moving from Running to Dead states;
suspend() (DEPRECATED) - Running to Waiting;
resume() (DEPRECATED) - Waiting to Ready.
 
Rahul Mahindrakar
Ranch Hand
Posts: 1869
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Great Work Jply.

------------------
Regds.
Rahul P. Mahindrakar
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks jply!
I �ll add the join method (that it�s behaviour may be similar to the wait method) and that a thread could end by an uncaught exception.
And the setPriority()? What do you mean by the preempted arrow?
An idea: perhaps you could make the diagram with different colors and for example color red implies platform dependent behaviours.
Another thing that I don�t see clearly is the release of the lock by calling the wait() method.
[This message has been edited by Marcela Blei (edited September 08, 2000).]
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All;
Is <code>join()</code> like <code>wait()</code> or more like <code>sleep()</code>, that is, does the thread have to reacquire any locks after the join ends? I've added a "joined" state, and some notes to address most of the other suggestions. I'm considering blocking on I/O (and on a @#&%! <code>MediaTracker</code> - I think I missed that question) to be special cases of entering synchronized code. Also, an uncaught exception is just another reason <code>run()</code> could exit.
I'm not ready to repost it yet, because of the colors issue. I'd have to make it HTML to do that. I don't want to use an image; I wanted something that could be posted directly without resorting to an image on a second web server. My text editor does block mode, so making it in text was easy. It's too much work to make it an HTML table manually (lions and rows and columns, oh my...), does anyone know of a free tool that'll do that automatically?
[This message has been edited by jply (edited September 08, 2000).]
 
Marcela Blei
Ranch Hand
Posts: 477
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is <code>join()</code> like <code>wait()</code> or more like <code>sleep()</code>, that is, does the thread have to reacquire any locks after the join ends?

I think that joins have nothing to do with locks. So it may be similar to sleep with it�s particular behaviour, I mean: sleep until the thread joined finishes.

I'm not ready to repost it yet, because of the colors issue. I'd have to make it HTML to do that. I don't want to use an image; I wanted something that could be posted directly without resorting to an image on a second web server. My text editor does block mode, so making it in text was easy. It's too much work to make it an HTML table manually (lions and rows and columns, oh my...), does anyone know of a free tool that'll do that automatically?

When I saw the UBB help there are no colors setting , but I remember I saw messages with colors so I search them and look how others did it by editing their messages
Edit my message to see how I applied color, is with the html font tag like: font color=
[This message has been edited by Marcela Blei (edited September 11, 2000).]
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Marcela, what do you mean by �platform dependent behaviour�? If method is native? If so, here they are:
Thread native methods:
public synchronized native void start();
public static native void yield();
public static native void sleep(long millis) throws InterruptedException;
private native boolean isInterrupted(boolean ClearInterrupted);
public final native boolean isAlive();
private native void setPriority0(int newPriority);
suspend, resume and interrupt are not declared as native, but they do nothing but call native methods:
private native void suspend0();
private native void resume0();
private native void interrupt0();
Thread non-native methods:
public final synchronized void join(long millis)
public final void stop()
public void destroy() // This method is not implemented
Object's native methods:
public final native void wait(long timeout) throws InterruptedException;
public final native void notify();
public final native void notifyAll();
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!