States of a Thread
If there is a thread, it can be in one of the following states.
- New
- Runnable
- Running
- Waiting /
Blocked / Sleeping - Dead
New state
In my previous post, I've mentioned how to create a thread( Extend Thread class or implements the Runnable interface). Once you create a thread instance, this instance is in this state. It means there is a thread instance, that is ready to run.
Runnable state
I think you can remember how to start a thread using start( ) method. Once you start( ) a thread instance, that thread is in Runnable state, but it cannot run until the thread scheduler selects that instance to run.
Running state
This is the actual running state of a thread, this state begins when the thread scheduler selects that thread to run.
Waiting / Blocked / Sleeping state
This is the state of stopping a thread from running state. Actually, there are three ways as I mentioned to stop a running thread. When you run one of the above methods, the thread will be not in Runnable state. But it can be forwarded to Runnable state with a special treat. There are three special methods which are used in this state.
sleep ( )
This is static method of Thread class . Here is the method declaration.
As you can see it throws InterruptedException. Once you call sleep( ) method, it may sleep your current thread for a given time duration that you mention in milli seconds . Look at the following example.
Demo.java
public class Demo extends Thread{ @Override public void run( ){ for (int i = 0;i < 10; i++){ System. out. println ( "Demo"); Thread. yield( );} } }
App.java
public class App{ public static void main( String[ ] args) { Demo demo = new Demo( ); Thread thread = new Thread( demo); try{ thread. start ( ); thread. sleep( 5000);} catch( InterruptedException e) { System. out. println ( e. getMessage ( ));} for( int i = 0;i < 10; i++) { System. out. println ( "Main");} } }
Once you run App.java it will print "Demo" 10 times and after 5s it will print "Main".
Yield( )
This can be used when a thread wants to pass its execution to give the chance for other remaining threads with the same priority.
public static void yield( )
It means yield( ) method change the state of currently executing thread to runnable state. But this depends on the platform, if you want to use this in a proper way, you have to have a supportive underlying processor. Otherwise, you may get confusing results. It means output cannot be guaranteed. Look at the following example.
It means yield
Demo.java
public class Demo extends Thread{ @Override public void run( ){ for (int i = 0;i < 5; i++){ System. out. println ( "Demo"); Thread. yield( );} } }
App.java
public class App{ public static void main( String[ ] args) throws InterruptedException{ Demo demo = new Demo( ); demo. start ( ); for( int i = 0;i < 5; i++){ System. out. println ( "Main");} } }
join ( )
Simply join
public class Demo extends Thread{ @Override public void run( ) { for( int i = 0;i < 5; i++) { System. out. println ( "Demo"); try{ Thread. sleep( 200);} catch( InterruptedException e) { e. printStackTrace ( );} } } }
App.java
public class App{ public static void main( String[ ] args) throws InterruptedException{ Demo demo = new Demo( ); demo. start ( ); demo. join ( ); for( int i = 0;i < 5; i++) { System. out. println ( "Main");} } }
Dead state
This is the end of a thread instance, once it becomes dead, it cannot be alive again.
I think now you've got some idea about the states of a thread. In the next post, I'l move with other related advanced theories in threads.
States of Threads
Reviewed by Ravi Yasas
on
8:58 AM
Rating:
No comments: