Java multithreading state

Java multithreading is an important feature in the Java language, which allows a program to run multiple threads at the same time, thereby improving the concurrent performance of the program. In Java multithreading, thread state is a very important concept, which represents the state of threads at different points in time. This article will introduce the concept of Java multi-thread state in detail, and how to use Java code to achieve different thread states.

Java multithreading state

The Java multi-thread state refers to the state of the thread at different points in time. The state of the thread in Java can be divided into the following six types:

  1. NEW: When using the new keyword to create a thread object, the thread is in the NEW state. In this state, the thread has not yet started executing, nor has it been started.

  2. RUNNABLE: When the thread's start() method is called, the thread enters the RUNNABLE state. In this state, the thread can be executed by the Java virtual machine, and can also be scheduled for execution by the thread scheduler of the operating system.

  3. BLOCKED: When the thread is waiting for a monitor lock, the thread enters the BLOCKED state. In this state, the thread is blocked and cannot execute.

  4. WAITING: A thread enters the WAITING state when a thread is waiting for another thread to perform a specific operation. In this state, the thread is blocked and cannot execute.

  5. TIMED_WAITING: When a thread waits for another thread to perform a specific operation and a timeout is set, the thread enters the TIMED_WAITING state. In this state, the thread is blocked, waiting for a timeout or notification from other threads.

  6. TERMINATED: When the thread completes its execution tasks, the thread enters the TERMINATED state. In this state, the thread has finished executing and will no longer execute.

Thread state sample code

Here is a sample program that demonstrates different thread states using Java code:

public class ThreadStateExample implements Runnable {
    
    

    public void run() {
    
    
        try {
    
    
            // 让线程睡眠 500 毫秒
            Thread.sleep(500);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
    
    
        Thread thread = new Thread(new ThreadStateExample());
        System.out.println("线程状态:" + thread.getState()); // NEW
        thread.start();
        System.out.println("线程状态:" + thread.getState()); // RUNNABLE
        try {
    
    
            Thread.sleep(100);
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("线程状态:" + thread.getState()); // TIMED_WAITING
        try {
    
    
            thread.join();
        } catch (InterruptedException e) {
    
    
            e.printStackTrace();
        }
        System.out.println("线程状态:" + thread.getState()); // TERMINATED
    }
}

In the above code, we created a class ThreadStateExample that implements the Runnable interface. In the run() method, we put the thread to sleep for 500 milliseconds. In the main() method, we create a new thread and start it. Before starting a thread, the thread is in the NEW state. After calling the start() method, the thread enters the RUNNABLE state. Then, we put the main thread to sleep for 100 milliseconds, waiting for the new thread to enter the TIMED_WAITING state. Finally, we use the join() method to wait for the new thread to finish executing and set the thread state to TERMINATED.

Summarize

This article introduces the concept of Java multithreading states and how to use Java code to implement different thread states. Java multi-thread state is a very important concept in Java multi-thread programming. Understanding the change of thread state can help us better control the execution process of threads and improve the concurrency performance of programs.

Guess you like

Origin blog.csdn.net/hj1993/article/details/131629141