天天看點

java中線程的生命周期_Java中的線程生命周期– Java中的線程狀态

java中線程的生命周期

Understanding Thread Life Cycle in Java and Thread States are very important when you are working with Threads and programming for multithreaded environment.

在使用線程和針對多線程環境進行程式設計時,了解Java和線程狀态下的 線程生命周期非常重要。

From our last tutorial, we can create a java thread class by implementing Runnable interface or by extending Thread class, but to start a java thread, we first have to create the Thread object and call it’s start() method to execute run() method as a thread.

從我們過去的教程中,我們可以建立一個Java線程通過實作Runnable接口或者擴充Thread類的類,但啟動Java線程,我們首先必須建立Thread對象并調用它的start()方法來執行run()方法作為線程。

Java中的線程生命周期 (Thread Life Cycle in Java)

Below diagram shows different states of thread life cycle in java. We can create a thread in java and start it but how the thread states change from Runnable to Running to Blocked depends on the OS implementation of thread scheduler and java doesn’t have full control on that.

下圖顯示了Java中線程生命周期的不同狀态。 我們可以在Java中建立一個線程并啟動它,但是線程狀态如何從Runnable更改為Running到Blocked取決于線程排程程式的OS實作,而Java對此沒有完全控制。

新 (New)

When we create a new Thread object using new operator, thread state is New Thread. At this point, thread is not alive and it’s a state internal to Java programming.

當我們使用new運算符建立新的Thread對象時,線程狀态為New Thread。 在這一點上,線程還沒有激活,它是Java程式設計内部的一種狀态。

可運作 (Runnable)

When we call start() function on Thread object, it’s state is changed to Runnable. The control is given to Thread scheduler to finish it’s execution. Whether to run this thread instantly or keep it in runnable thread pool before running, depends on the OS implementation of thread scheduler.

當我們在Thread對象上調用start()函數時,它的狀态更改為Runnable。 将控制權交給線程排程程式以完成其執行。 是立即運作此線程還是在運作之前将其保留在可運作線程池中,取決于線程排程程式的OS實作。

跑步 (Running)

When thread is executing, it’s state is changed to Running. Thread scheduler picks one of the thread from the runnable thread pool and change it’s state to Running. Then CPU starts executing this thread. A thread can change state to Runnable, Dead or Blocked from running state depends on time slicing, thread completion of run() method or waiting for some resources.

線程執行時,其狀态更改為正在運作。 線程排程程式從可運作線程池中選擇一個線程,并将其狀态更改為“正在運作”。 然後,CPU開始執行該線程。 線程可以将時間從運作狀态更改為Runnable,Dead或Blocked,這取決于時間切片,run()方法的線程完成或等待某些資源。

封鎖/等待 (Blocked/Waiting)

A thread can be waiting for other thread to finish using thread join or it can be waiting for some resources to available. For example producer consumer problem or waiter notifier implementation or IO resources, then it’s state is changed to Waiting. Once the thread wait state is over, it’s state is changed to Runnable and it’s moved back to runnable thread pool.

一個線程可以等待其他線程使用線程連接配接完成,也可以等待一些可用資源。 例如生産者消費者問題或服務員通知程式實作或IO資源,則其狀态更改為等待。 線程等待狀态結束後,其狀态将更改為Runnable,并将其移回可運作線程池。

死 (Dead)

Once the thread finished executing, it’s state is changed to Dead and it’s considered to be not alive.

線程執行完畢後,其狀态将更改為Dead(死),并且認為該狀态不活躍。

Above are the different states of thread. It’s good to know them and how thread changes it’s state. That’s all for thread life cycle in java.

以上是線程的不同狀态 。 了解它們以及線程如何更改其狀态非常好。 這就是Java中線程生命周期的全部内容。

翻譯自: https://www.journaldev.com/1044/thread-life-cycle-in-java-thread-states-in-java

java中線程的生命周期