java 并發線程
In the Java language, multithreading is driven by the core concept of a Thread. During their lifecycle, threads go through various stages.
在Java語言中,多線程由線程的核心概念驅動。 線上程的生命周期中,它會經曆各個階段。
Understanding Thread Life Cycle and the Thread States are very important when you are working with multithreaded programming.
當您使用多線程程式設計時,了解線程生命周期和線程狀态非常重要。
To understand thread states and when the thread is moving between these states we will use a diagram:
為了了解線程狀态以及線程在這些狀态之間移動的時間,我們将使用一個圖表:

On this diagram, you can see all the possible states of a Thread and conditions when these states are changing. To make it more clear we will go through all these states. But before let's check what each state means.
在此圖上,您可以檢視線程的所有可能狀态以及這些狀态更改時的條件。 更清楚地說,我們将經曆所有這些狀态。 但是在檢查每個狀态意味着什麼之前。
線程狀态 (Thread states)
The java.lang.Thread class contains a static State enum — which defines its potential states. The thread can only be in one of these states at a time:
java.lang.Thread類包含一個靜态 枚舉 —定義其潛在狀态。 線程一次隻能處于以下狀态之一:
-
NEW — a newly created thread that has not yet started the execution
NEW —尚未開始執行的新建立的線程
-
RUNNABLE — either running or ready for execution but it’s waiting for resource allocation
RUNNABLE —正在運作或準備執行,但正在等待資源配置設定
-
WAITING — waiting for some other thread to perform a particular action without any time limit
等待中—等待其他線程執行特定操作而沒有任何時間限制
-
TIMED_WAITING — waiting for some other thread to perform a specific action for a specified period
TIMED_WAITING —等待其他線程在指定時間段内執行特定操作
-
BLOCKED — waiting to acquire a lock to enter or re-enter a synchronized block/method
已阻止-等待擷取鎖以進入或重新進入同步塊/方法
-
TERMINATED — has completed its execution
TERMINATED-已完成執行
Now we can go through all these states.
現在我們可以經曆所有這些狀态。
NEW
新
A NEW Thread is a thread that’s been created but not yet started. It remains in this state until we start it using the start() method.
NEW Thread是已建立但尚未啟動的線程。 它将保持這種狀态,直到我們使用start()方法啟動它為止。
The following code snippet shows a newly created thread that’s in the NEW state:
以下代碼段顯示了一個新建立的處于NEW狀态的線程:
Thread t = new MyThread();
RUNNABLE
可運作
When we’ve created a new thread and called the start() method on that, it’s moved from NEW to RUNNABLE state. Threads in this state are either running or ready to run, but they’re waiting for resource allocation from the system.
當我們建立了一個新線程并在其上調用start()方法時,它已從NEW變為RUNNABLE狀态。 處于此狀态的線程正在運作或可以運作,但是它們正在等待系統配置設定資源。
In a multi-threaded environment, the Thread-Scheduler (which is part of JVM) allocates a fixed amount of time to each thread. So it runs for a particular amount of time, then passes the control to other RUNNABLE threads.
在多線程環境中,線程排程程式(JVM的一部分)為每個線程配置設定固定的時間量。 是以它運作特定的時間,然後将控件傳遞給其他RUNNABLE線程。
The following code snippet shows a newly created thread that’s in the RUNNABLE state:
以下代碼片段顯示了一個新建立的處于RUNNABLE狀态的線程:
Thread t = new MyThread();
t.start();
WAITING
等候
A thread is in WAITING state when it’s waiting for some other thread to perform a particular action. According to JavaDocs, any thread can enter this state by calling any one of the following three methods:
等待其他線程執行特定操作時,該線程處于WAITING狀态。 根據JavaDocs的說法 ,任何線程都可以通過調用以下三種方法之一來進入此狀态:
-
object.wait()
object.wait()
-
thread.join() or
thread.join()或
-
LockSupport.park()
LockSupport.park()
Here is an example:
這是一個例子:
What was happened:
發生了什麼事:
-
We’ve created and started a thread — threadA
我們建立并啟動了一個線程— threadA
-
threadA creates a threadB and starts it
threadA建立一個threadB并啟動它
-
Inside threadA we called threadB.join(), this puts threadA in WAITING state until threadB has finished execution
在threadA内部,我們稱為threadB.join() ,這将線程A置于等待狀态,直到線程B完成執行
TIMED WAITING
定時等待
A thread is in TIMED_WAITING state when it’s waiting for another thread to perform a particular action within a specified amount of time.
當一個線程正在等待另一個線程在指定的時間内執行特定操作時,該線程處于TIMED_WAITING狀态。
According to JavaDocs, there are five ways to put a thread on TIMED_WAITING state:
根據JavaDocs的介紹 ,有五種方法可以将線程置于TIMED_WAITING狀态:
-
thread.sleep(long millis)
thread.sleep(長毫秒)
-
wait(int timeout) or wait(int timeout, int nanos)
wait(int timeout)或wait(int timeout,int nanos)
-
thread.join(long millis)
thread.join(長毫秒 )
-
LockSupport.parkNanos
LockSupport.parkNanos
-
LockSupport.parkUntil
LockSupport.parkUntil
What was happened:
發生了什麼事:
-
We’ve created and started a threadA
我們建立并啟動了一個線程
-
Move to sleep main thread
進入睡眠主線程
-
threadA start to processing
線程開始處理
-
Method sleep is called — threadA in TIMED_WAITING
調用方法sleep — TIMED_WAITING中的 threadA
BLOCKED
已封鎖
A thread is in the BLOCKED state when it’s currently not eligible to run. It enters this state when it is waiting for a lock and is trying to access a section of code that is locked by some other thread.
目前不符合運作條件的線程處于“ 阻塞”狀态。 當它等待鎖并嘗試通路被某個其他線程鎖定的一段代碼時,它将進入此狀态。
Here is an example:
這是一個例子:
What was happened:
發生了什麼事:
-
We’ve created two different threads — threadA and threadB
我們建立了兩個不同的線程-threadA和threadB
-
One of the threads entered synchronized infinityMethod() method(there is no guaranty that threadA will run this method first) this means that only one thread can access it; all other threads that try to access this method will be blocked from the further execution until the current one will finish the processing
輸入的線程之一同步了infinityMethod()方法(沒有保證threadA首先運作該方法),這意味着隻有一個線程可以通路它; 嘗試通路此方法的所有其他線程将被阻止進一步執行,直到目前線程将完成處理為止
-
The second thread will try to access infinityMethod() but this method is in use by the first thread — the second thread will be BLOCKED
第二個線程将嘗試通路infinityMethod(),但第一個線程正在使用此方法-第二個線程将被阻塞
TERMINATED
已終止
This is the state of a dead thread. It’s in the TERMINATED state when it has either finished execution or was terminated abnormally.
這是死線程的狀态。 完成執行或異常終止時,它處于TERMINATED狀态。
Here is an example:
這是一個例子:
Here, while we’ve started thread t1, the very next statement Thread.sleep(1000) gives enough time for t1 to complete and so this program gives us the output as:
在這裡,當我們啟動線程t1時 ,下一條語句Thread.sleep(1000)為t1完成提供了足夠的時間,是以該程式将輸出顯示為:
What was happened:
發生了什麼事:
-
We’ve created and started a threadA
我們建立并啟動了一個線程
-
Move to sleep main thread
進入睡眠主線程
-
threadA start to processing
線程開始處理
-
threadA is finished — became in state TERMINATED
線程A完成-進入狀态TERMINATED
結論 (Conclusion)
In this article, we learned thread states and how to switch between all states.
在本文中,我們學習了線程狀态以及如何在所有狀态之間切換。
In the next article, we will check How to create a thread. Stay tuned.
在下一篇文章中,我們将檢查如何建立線程 。 敬請關注。
Found this article useful? Follow me (Dmytro Timchenko) on Medium and check out my other articles below! Please 👏 this article to share it!
覺得這篇文章有用嗎? 在Medium上關注我( Dmytro Timchenko ),并在下面檢視我的其他文章! 請👏這篇文章分享!
Java Concurrency
Java并發
Spring RESTful: Hibernate
SpringRESTful:Hibernate
Spring RESTful: Docker, PostgreSQL, Liquibase
SpringRESTful:Docker,PostgreSQL,Liquibase
翻譯自: https://medium.com/javarevisited/java-concurrency-thread-life-cycle-4869432474b
java 并發線程