Oh Why Its This Thread Again

A thread in Java at whatever point of time exists in any one of the following states. A thread lies but in i of the shown states at any instant:

  1. New
  2. Runnable
  3. Blocked
  4. Waiting
  5. Timed Waiting
  6. Terminated

The diagram shown beneath represents various states of a thread at whatever instant in time.

Life Wheel of a thread

  1. New Thread: When a new thread is created, information technology is in the new country. The thread has non yet started to run when the thread is in this land. When a thread lies in the new state, its code is yet to be run and hasn't started to execute.
  2. Runnable State: A thread that is ready to run is moved to a runnable state. In this land, a thread might actually be running or it might be gear up to run at any instant of fourth dimension. Information technology is the responsibility of the thread scheduler to give the thread, time to run.
    A multi-threaded plan allocates a fixed corporeality of time to each individual thread. Each and every thread runs for a short while and and then pauses and relinquishes the CPU to another thread so that other threads tin get a gamble to run. When this happens, all such threads that are prepare to run, waiting for the CPU and the currently running thread lie in a runnable state.
  3. Blocked/Waiting state: When a thread is temporarily inactive, then information technology's in one of the following states:
    • Blocked
    • Waiting
  4. Timed Waiting: A thread lies in a timed waiting land when it calls a method with a time-out parameter. A thread lies in this state until the timeout is completed or until a notification is received. For example, when a thread calls sleep or a provisional look, it is moved to a timed waiting land.
  5. Terminated State: A thread terminates considering of either of the following reasons:
    • Because information technology exits normally. This happens when the code of the thread has been entirely executed by the program.
    • Because at that place occurred some unusual erroneous issue, like segmentation fault or an unhandled exception.

Implementing the Thread States in Coffee

In Coffee, to get the current state of the thread, use Thread.getState() method to go the current state of the thread. Java provides coffee.lang.Thread.State course that defines the ENUM constants for the state of a thread, as a summary of which is given beneath:

1. New

Declaration: public static final Thread.State NEW

Description: Thread state for a thread that has non yet started.

2. Runnable

Annunciation: public static terminal Thread.State RUNNABLE

Description: Thread state for a runnable thread. A thread in the runnable state is executing in the Coffee virtual machine merely it may be waiting for other resource from the operating system such as a processor.

3. Blocked

Declaration: public static concluding Thread.State BLOCKED

Description: Thread state for a thread blocked waiting for a monitor lock. A thread in the blocked land is waiting for a monitor lock to enter a synchronized block/method or reenter a synchronized block/method later calling Object.wait().

4. Waiting

Declaration: public static final Thread.Land WAITING

Clarification: Thread country for a waiting thread. Thread country for a waiting thread. A thread is in the waiting land due to calling one of the following methods:

  • Object.wait with no timeout
  • Thread.join with no timeout
  • LockSupport.park

v. Timed Waiting

Proclamation: public static final Thread.State TIMED_WAITING

Description: Thread country for a waiting thread with a specified waiting fourth dimension. A thread is in the timed waiting state due to calling one of the following methods with a specified positive waiting fourth dimension:

  • Thread.slumber
  • Object.wait with timeout
  • Thread.join with timeout
  • LockSupport.parkNanos
  • LockSupport.parkUntil

6. Terminated

Annunciation: public static final Thread.State TERMINATED

Clarification: Thread country for a terminated thread. The thread has completed execution.

Java

grade thread implements Runnable {

public void run()

{

effort {

Thread.sleep( 1500 );

}

catch (InterruptedException east) {

e.printStackTrace();

}

System.out.println(

"Land of thread1 while it called bring together() method on thread2 -"

+ Examination.thread1.getState());

try {

Thread.sleep( 200 );

}

catch (InterruptedException e) {

e.printStackTrace();

}

}

}

public grade Examination implements Runnable {

public static Thread thread1;

public static Test obj;

public static void principal(Cord[] args)

{

obj = new Test();

thread1 = new Thread(obj);

Organisation.out.println(

"State of thread1 later on creating it - "

+ thread1.getState());

thread1.start();

System.out.println(

"State of thread1 after calling .start() method on it - "

+ thread1.getState());

}

public void run()

{

thread myThread = new thread();

Thread thread2 = new Thread(myThread);

Arrangement.out.println(

"Country of thread2 later creating it - "

+ thread2.getState());

thread2.start();

Organization.out.println(

"State of thread2 after calling .start() method on it - "

+ thread2.getState());

try {

Thread.sleep( 200 );

}

take hold of (InterruptedException due east) {

e.printStackTrace();

}

System.out.println(

"State of thread2 later on calling .sleep() method on information technology - "

+ thread2.getState());

endeavor {

thread2.join();

}

grab (InterruptedException e) {

east.printStackTrace();

}

System.out.println(

"State of thread2 when information technology has finished it's execution - "

+ thread2.getState());

}

}

Output

State of thread1 after creating it - NEW Land of thread1 after calling .start() method on it - RUNNABLE Land of thread2 after creating information technology - NEW State of thread2 subsequently calling .start() method on it - RUNNABLE State of thread2 after calling .sleep() method on it - TIMED_WAITING State of thread1 while information technology chosen join() method on thread2 -WAITING State of thread2 when it has finished it'south execution - TERMINATED

Explanation: When a new thread is created, the thread is in the NEW state. When the start() method is called on a thread, the thread scheduler moves it to Runnable state. Whenever the bring together() method is called on a thread case, the current thread executing that statement volition expect for this thread to move to the Terminated state. Then, before the last argument is printed on the console, the program calls join() on thread2 making the thread1 wait while thread2 completes its execution and is moved to the Terminated land. thread1 goes to Waiting state considering it is waiting for thread2 to consummate its execution equally it has called join on thread2.

This article is contributed by Mayank Kumar. If you lot like GeeksforGeeks and would like to contribute, you lot can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. Run into your article appearing on the GeeksforGeeks main folio and assist other Geeks.

Please write comments if you find anything wrong, or yous want to share more information about the topic discussed to a higher place.


mickelnetter.blogspot.com

Source: https://www.geeksforgeeks.org/lifecycle-and-states-of-a-thread-in-java/

0 Response to "Oh Why Its This Thread Again"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel