life cycle of thread


0  0 2016-10-01 02:34:06

But for better understanding purpose the threads life cycle, we are explaining it in the five states.
The Thread life cycle of java is handled / controlled by Java Virtual Machine (JVM). Below are the  java thread states:
1. New State
2. Runnable State
3. Running State
4. Non-Runnable (Blocked) State
5. Terminated State

1) New State                             : If we create or define an instance of thread class but before the invocation / run of start() method the thread is now in the new state.
2) Runnable State                      : Invocation of start() method the thread is in runnable state, but thread scheduler has not selected / declare it to be the running thread.
3) Running State                        : If the thread scheduler has selected it then the thread is in running state.
4) Non-Runnable (Blocked) State : Currently not eligible to run the thread but thread is still alive so this time it is in non-runnable state.
5) Terminated State                    : When threads run() method exits then thread is in terminated or dead state.

Thread class Constructors:
• Thread()                  :- default constructor.
• Thread(String name) :- this constructor required one string parameter.
• Thread(Runnable r)   :- it is used the Runnable interfece object as a parameter.
• Thread(Runnable r,String name)

Thread class Methods :-
1. run():  this method is used to perform execution / action for a thread.
2. start(): actually starts the execution of the thread. Java Virtual Machine calls the run() method on the thread.
3. sleep(long milliseconds(1000) ): suspend the currently running / executing thread to sleep (temporarily stop the execution) for the specified number of time which is in milliseconds.
4. join(): this method is waits for thread to die.
5. join(long milliseconds(1000)): this method waits for thread to die for the specified time which is in miliseconds.
6.  int getPriority(): this method returns priority of the current thread.
7. int setPriority(int priority): this method changes the priority of the current thread.
8. String getName(): this method returns the name of the current thread.
9. setName(String name): this method changes the name of the thread. getId(): this method returns the id of the thread.
11.boolean isAlive(): this method check / tests if the thread is alive.
12.suspend(): this method suspend the thread.
13.stop(): this method is used to stop the thread execution.
14.boolean isDaemon(): this method tests if the thread is a daemon thread or normal.
15.setDaemon(boolean b): this method makes / marks the thread as daemon or user thread.
16.interrupt(): this method interrupts the thread.
17.boolean isInterrupted(): this method tests if the thread has been interrupted.
18.static boolean interrupted(): this method tests if the current thread has been interrupted.


Comments (0)

Post Your Comments


Hong Kong Office

Find Us On