Home > Java > javaTutorial > What are the thread states in Java?

What are the thread states in Java?

小老鼠
Release: 2024-01-16 16:46:49
Original
638 people have browsed it

The thread states are: 1. New state: After the thread object is created, it enters the new state. For example, Thread thread=new Thread(); 2. Ready state: also called "executable state"; 3. Running state: the thread obtains CPU permissions to execute; 4. Blocked state: the thread gives up CPU use for some reason Right, temporarily stop running; 5. Waiting state: need to wait for other threads to make some specific actions; 6. Timeout waiting state: can return by itself at the specified time; 7. Termination state, etc.

What are the thread states in Java?

Operating system for this tutorial: Windows 10 system, Dell G3 computer.

Java thread states mainly include the following:

1. New state (New): After the thread object is created, it enters the new state. For example, Thread thread = new Thread().

2. Ready state (Runnable): also known as "executable state". After the thread object is created, other threads call the object's start() method to start the thread. For example, thread.start(). Threads in the ready state may be scheduled for execution by the CPU at any time.

3. Running status (Running): The thread obtains CPU permissions for execution. It should be noted that a thread can only enter the running state from the ready state.

4. Blocked state (Blocked): The blocked state means that the thread gives up the right to use the CPU for some reason and temporarily stops running. Until the thread enters the ready state, it has a chance to move to the running state. The blocking state can be divided into the following types:

a) Synchronous blocking: When the running thread acquires the synchronization lock of the object, and the synchronization lock is occupied by other threads, the JVM will put the thread into the lock pool middle.

b) Waiting for blocking: The running thread executes the wait() method, and the JVM will put the thread into the waiting pool.

c) Other blocking: When the running thread executes the sleep() method or join() method, or issues an I/O request, the JVM will put the thread in a blocked state.

5. Waiting state (Waiting): The thread entering this state needs to wait for other threads to take some specific actions (notification or interrupt).

6. Timeout waiting state (Timed_Waiting): This state is different from Waiting in that it can return by itself at the specified time.

7. Terminated status (Terminated): Indicates that the thread has completed execution, and the threads in the system have completed execution and been destroyed, but the Thread object is still there. System.out.println(t.getState()); } }

The above are several thread states in Java. It should be noted that a thread may experience these states during its life cycle. These The state also constitutes the life cycle of the thread. At the same time, the transition rules of various states are also very important. Understanding these rules is very important for writing correct and efficient Java multi-threaded programs.

The above is the detailed content of What are the thread states in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template