The life cycle of Java threads and the example code are described in detail

  • 2020-05-10 18:15:48
  • OfStack

When a thread is created and started, it is neither in the execution state when it is started, nor in the execution state directly. In its life cycle, it passes through five states: "new" (New), "ready" (Runnable), "run" (Running'), "blocked" (Blocked), and "dead" (Dead). After the creation of the thread, it is impossible for the thread 1 to monopolize CPU to run independently, so it needs to switch between multiple threads, so it is switched between running and blocking most of the time.

  1. Thread state

Threads exist in several different states, as follows:

New state Ready state Running state Dead state Non Runnable state

1. State of New

The New state is the state in which the thread has been created but has not yet started running. This state makes the thread run by calling the thread's start() method.

2. Runnable status

The Runnable state, which can be called the ready to run state or the queue, lets the thread run by calling the start() method.
The thread scheduler determines which threads to run and for how long.

3. Running state

If a thread is executing, it is in the Running state.

4. Dead state

Once a thread enters the Dead state, it can no longer run.

5. Non runnable status

A running thread can transition to the Non runnable state, depending on the run. A thread can also hold the Non runnable state until a condition is met. A thread in the Non runnable state cannot jump directly to the run state, but must first transition to the Runnable state. Sleep Sleeping: a thread sleeps for a specified time. I/O block: the thread waits until the block operation is completed. join block: a thread waits until another thread completes execution. Wait notification: a thread waits for notification from another thread. Lock mechanism blocks: the thread waits until the specified lock is released to acquire the lock.

Java virtual machine JVM executes threads according to thread priority and scheduling principles.

2. Thread scheduler

In JVM, the implementation of the thread scheduler is usually based on the following two policies:

Preemption scheduling policy
Time-sharing scheduling policy or Round-robin round-robin scheduling policy

The implementation of the thread scheduler is platform-independent, so the scheduling of threads is unpredictable.

3. Thread priority

JVM assigns a priority to each newly created thread.

Level 0: this is the lowest priority
Level 5: this is a normal priority
Level 10: this is the highest priority

To save these values, the thread class has three variables:

public static final int MIN_PRIORITY public static final int NORM_PRIORITY public static final int MAX_PRIORITY

A thread will first inherit the priority of its parent thread. The default priority of each thread is level 5 (Normal priority), and the default priority of the main thread is level 5.

You can set the priority of threads by using the setPriority(int priority) method.

public final void setPriority(int priority)
public void getPriority();

For user-defined threads, the default thread name is Thread+ serial number, and the serial number starts from 0. For example, the first thread is Thread0.
The thread name can be set using the setName(String name) method, and the name of the thread can be obtained using the getName() method.

public final void setName(String name)
public final String getName().

The instance

Here's an example:


package demo.ch;

public class UserThread extends Thread {
  UserThread() {
    super();
  }

  UserThread(String name) {
    super(name);
  }

  public void run() {
    System.out.println("thread started running..");
  }

  public static void main(String[] args) {
    UserThread thread1 = new UserThread("Thread1");
    UserThread thread2 = new UserThread("Thread2");

    System.out.println("Thread 1 initial name and priority");
    System.out.println("name:" + thread1.getName());
    System.out.println("priority:" + thread1.getPriority());

    System.out.println("Thread 2 initial name and priority");
    System.out.println("name:" + thread2.getName());
    System.out.println("priority:" + thread2.getPriority());
    System.out.println("");

    thread1.setPriority(6);
    thread2.setPriority(9);

    System.out.println("Thread 1 initial name and priority");
    System.out.println("name:" + thread1.getName());
    System.out.println("priority:" + thread1.getPriority());

    System.out.println("Thread 2 initial name and priority");
    System.out.println("name:" + thread2.getName());
    System.out.println("priority:" + thread2.getPriority());
    System.out.println("");

    thread1.start();
    thread2.start();

    for(int i=0; i<5; i++)
      System.out.println("main method i value: " + i);
  }
}

Output results:


Thread 1 initial name and priority
name:Thread1
priority:5
Thread 2 initial name and priority
name:Thread2
priority:5

Thread 1 initial name and priority
name:Thread1
priority:6
Thread 2 initial name and priority
name:Thread2
priority:9

main method i value: 0
main method i value: 1
thread started running..
main method i value: 2
thread started running..
main method i value: 3
main method i value: 4

Thank you for reading, I hope to help you, thank you for your support of this site!


Related articles: