Home  >  Article  >  Java  >  Advanced knowledge of java - six states of threads

Advanced knowledge of java - six states of threads

王林
王林forward
2019-11-26 11:44:593195browse

Advanced knowledge of java - six states of threads

Thread status

The thread has the following 6 states:

New(newly created )

Runnable(runnable)

Blocked(blocked)

Waiting(waiting )

Timed waiting(Timed waiting)

Terminated(Terminated)

Among them, to obtain the current status of a thread State, you can call the getState method.

java related video recommendations: java video

Below, each status is explained

1. Newly created thread (New)

When we use the new operator to create a thread, such as when using new Thread (r), the thread has not started running, and the code in the thread has not started executing. At this time, it becomes New status.

2. Runnable thread (Runnable)

When we execute the start() method on a thread, the thread will be started. But note that a thread in a runnable state may or may not be running, depending on the operating system's time slice scheduling. The preemptive scheduling system allocates a time slice to each thread to run. After the time slice is used up, the operating system will deprive it of the right to run and select the next thread to run based on priority.

3. Blocked, waiting state (Blocked, waiting, Timed waiting)

When a thread is blocked and waiting, it does not do any work, and No code is executed. until the operating system reactivates it.

When a thread attempts to acquire an internal object lock, and the lock is held by another thread, the thread enters the blocking state. When the other thread releases the lock, and the thread scheduler allows the thread to hold With it, the thread will become non-blocking.

When a thread waits for another thread to notify the scheduler of a condition, it enters the waiting state, and then calls the Object.wait method or Thread.join method. This situation occurs. There is a big difference between the blocked state and the waiting state.

Several methods have a timeout parameter. Calling them causes the thread to enter a timed wait state, which is retained until the timeout expires or an appropriate notification is received. Methods with timeout parameters are: Thread.sleep and Object.wait, Thread.join, Lock, tryLock and ## Timed version of #Condition.await.

4. Terminated thread

There are two reasons for thread termination:

Because the run() method exits normally after execution

Unexpected death due to terminating the run method due to an uncaught exception

5. Summary

Advanced knowledge of java - six states of threads

Recommended related articles and tutorials:

Introduction to java programming

The above is the detailed content of Advanced knowledge of java - six states of threads. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete