Java thread status

1. Thread status

A Java thread may be in 6 different states during its running life cycle as shown in the figure below. A thread can only be in one of the states at a given moment.

Java thread status
Status name illustrate
NEW In the initial state, the thread is constructed, but the start() method has not been called yet.
RUNNABLE Running state. Java threads generally refer to the two states of ready and running in the operating system as running.
BLOCKED Blocked state, indicating that the thread is blocked in the lock
WAITING Waiting state means that the thread enters the waiting state. Entering this state means that the current thread needs to wait for other threads to make some specific actions (notification or interruption)
TIME WAITING Timeout waiting state, this state is different from WAITING, it can return by itself at the specified time
TERMINATED Termination status, indicating that the current thread has completed execution

2. Code demonstration

1. Threadstate class

public class Threadstate {
    
    
    public static void main(String args[]){
    
    
        new Thread(new TimeWaiting(),"TimeWaitingThread").start();
        new Thread(new Waiting(),"WaitingThread").start();
        //使用两个Blocked线程,一个获取锁成功,另一个被阻塞
        new Thread(new Blocked(),"BlockedThread-1").start();
        new Thread(new Blocked(),"BlockedThread-2").start();

    }
    // 该线程不断地进行睡眠
    static class TimeWaiting implements Runnable{
    
    
        @Override
        public void run() {
    
    
            while (true){
    
    
                SleepUtils.second(100);
            }
        }
    }
    // 该线程在Waiting.class实例上等待
    static class Waiting implements Runnable{
    
    

        @Override
        public void run() {
    
    
            while (true){
    
    
                synchronized (Waiting.class){
    
    
                    try{
    
    
                        Waiting.class.wait();
                    } catch (InterruptedException interruptedException) {
    
    
                        interruptedException.printStackTrace();
                    }
                }
            }
        }
    }
    //该线程在Blocked.class实例上加锁后,不会释放该锁
    static class Blocked implements Runnable{
    
    
        @Override
        public void run() {
    
    
            synchronized (Blocked.class){
    
    
                while (true){
    
    
                    SleepUtils.second(100);
                }
            }
        }
    }
}

2. SleepUtils class

public class SleepUtils {
    
    
    public static final void second(long seconds){
    
    
        try{
    
    
            TimeUnit.SECONDS.sleep(seconds);
        } catch (InterruptedException interruptedException) {
    
    
            interruptedException.printStackTrace();
        }
    }
}

3. Run the example

To run this example, open a terminal or Windows prompt and type jps. The output is as follows:

jps
You can see that the process ID corresponding to the running example is 13948, and then type jstack 13948, as follows:

process
Through examples, we understand the specific meaning of thread status when a Java program is running.

The thread is not in a fixed state during its own life cycle, but switches between different states as the code is executed. The Java thread state changes as shown in the figure below: As can be seen from the figure above, thread
Thread status flow
creation After that, call the start() method to start running. When the thread executes the wait() method, the thread enters the waiting state. Threads that enter the waiting state need to rely on notifications from other threads to return to the running state. The timeout waiting state is equivalent to adding a timeout limit on the basis of the waiting state, that is, it will return to the running state when the timeout period is reached. When a thread calls a synchronization method, the thread will enter a blocking state without acquiring the lock. The thread will enter the terminated state after executing the Runnable's run() method.

Precautions:

  • Java combines the two states of running and ready in the operating system and calls it the running state.

  • The blocking state is the state when the thread is blocked when entering the method or code block (acquiring the lock) modified by the synchronized keyword, but the thread state blocked in the Lock interface in the java.concurrent package is in the waiting state, because the Lock interface in the iava.concurrent package For the implementation of blocking, the relevant methods in the LockSupport class are used.

3. Reference materials

The art of concurrent programming in Java

Guess you like

Origin blog.csdn.net/qq_39939541/article/details/132391911