摘要:一文帶你搞懂執行緒、多執行緒和執行緒池。
一.執行緒
在作業系統中,執行緒是比程式更小的能夠獨立執行的基本單位。同時,它也是CPU排程的基本單位。執行緒本身基本上不擁有系統資源,只是擁有一些在執行時需要用到的系統資源,例如程式計數器,暫存器和棧等。一個程式中的所有執行緒可以共享程式中的所有資源。
二.多執行緒
多執行緒可以理解為在同一個程式中能夠同時執行多個不同的執行緒來執行不同的任務,這些執行緒可以同時利用CPU的多個核心執行。多執行緒程式設計能夠最大限度的利用CPU的資源。如果某一個執行緒的處理不需要佔用CPU資源時(例如IO執行緒),可以使當前執行緒讓出CPU資源來讓其他執行緒能夠獲取到CPU資源,進而能夠執行其他執行緒對應的任務,達到最大化利用CPU資源的目的。
三.執行緒的實現方式
在Java中,實現執行緒的方式大體上分為三種,通過繼承Thread類、實現Runnable介面,實現Callable介面。簡單的示例程式碼分別如下所示。
1)繼承Thread類程式碼
package io.binghe.concurrent.executor.test; /** * @author binghe * @version 1.0.0 * @description 繼承Thread實現執行緒 */ public class ThreadTest extends Thread { @Override public void run() { //TODO 在此寫線上程中執行的業務邏輯 } }
2)實現Runnable介面的程式碼
package io.binghe.concurrent.executor.test; /** * @author binghe * @version 1.0.0 * @description 實現Runnable實現執行緒 */ public class RunnableTest implements Runnable { @Override public void run() { //TODO 在此寫線上程中執行的業務邏輯 } }
3)實現Callable介面的程式碼
package io.binghe.concurrent.executor.test; import java.util.concurrent.Callable; /** * @author binghe * @version 1.0.0 * @description 實現Callable實現執行緒 */ public class CallableTest implements Callable<String> { @Override public String call() throws Exception { //TODO 在此寫線上程中執行的業務邏輯 return null; } }
四.執行緒的生命週期
1)生命週期
一個執行緒從建立,到最終的消亡,需要經歷多種不同的狀態,而這些不同的執行緒狀態,由始至終也構成了執行緒生命週期的不同階段。執行緒的生命週期可以總結為下圖。
其中,幾個重要的狀態如下所示。
- NEW:初始狀態,執行緒被構建,但是還沒有呼叫start()方法。
- RUNNABLE:可執行狀態,可執行狀態可以包括:執行中狀態和就緒狀態。
- BLOCKED:阻塞狀態,處於這個狀態的執行緒需要等待其他執行緒釋放鎖或者等待進入synchronized。
- WAITING:表示等待狀態,處於該狀態的執行緒需要等待其他執行緒對其進行通知或中斷等操作,進而進入下一個狀態。
- TIME_WAITING:超時等待狀態。可以在一定的時間自行返回。
- TERMINATED:終止狀態,當前執行緒執行完畢。
2)程式碼示例
為了更好的理解執行緒的生命週期,以及生命週期中的各個狀態,接下來使用程式碼示例來輸出執行緒的每個狀態資訊。
-
WaitingTime
建立WaitingTime類,在while(true)迴圈中呼叫TimeUnit.SECONDS.sleep(long)方法來驗證執行緒的TIMED_WARTING狀態,程式碼如下所示。
package io.binghe.concurrent.executor.state; import java.util.concurrent.TimeUnit; /** * @author binghe * @version 1.0.0 * @description 執行緒不斷休眠 */ public class WaitingTime implements Runnable{ @Override public void run() { while (true){ waitSecond(200); } } //執行緒等待多少秒 public static final void waitSecond(long seconds){ try { TimeUnit.SECONDS.sleep(seconds); } catch (InterruptedException e) { e.printStackTrace(); } } }
-
WaitingState
建立WaitingState類,此執行緒會在一個while(true)迴圈中,獲取當前類Class物件的synchronized鎖,也就是說,這個類無論建立多少個例項,synchronized鎖都是同一個,並且執行緒會處於等待狀態。接下來,在synchronized中使用當前類的Class物件的wait()方法,來驗證執行緒的WAITING狀態,程式碼如下所示。
package io.binghe.concurrent.executor.state; /** * @author binghe * @version 1.0.0 * @description 執行緒在Warting上等待 */ public class WaitingState implements Runnable { @Override public void run() { while (true){ synchronized (WaitingState.class){ try { WaitingState.class.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
-
BlockedThread
BlockedThread主要是在synchronized程式碼塊中的while(true)迴圈中呼叫TimeUnit.SECONDS.sleep(long)方法來驗證執行緒的BLOCKED狀態。當啟動兩個BlockedThread執行緒時,首先啟動的執行緒會處於TIMED_WAITING狀態,後啟動的執行緒會處於BLOCKED狀態。程式碼如下所示。
package io.binghe.concurrent.executor.state; /** * @author binghe * @version 1.0.0 * @description 加鎖後不再釋放鎖 */ public class BlockedThread implements Runnable { @Override public void run() { synchronized (BlockedThread.class){ while (true){ WaitingTime.waitSecond(100); } } } }
-
ThreadState
啟動各個執行緒,驗證各個執行緒輸出的狀態,程式碼如下所示。
package io.binghe.concurrent.executor.state; /** * @author binghe * @version 1.0.0 * @description 執行緒的各種狀態,測試執行緒的生命週期 */ public class ThreadState { public static void main(String[] args){ new Thread(new WaitingTime(), "WaitingTimeThread").start(); new Thread(new WaitingState(), "WaitingStateThread").start(); //BlockedThread-01執行緒會搶到鎖,BlockedThread-02執行緒會阻塞 new Thread(new BlockedThread(), "BlockedThread-01").start(); new Thread(new BlockedThread(), "BlockedThread-02").start(); } }
執行ThreadState類,如下所示。
可以看到,未輸出任何結果資訊。可以在命令列輸入“jps”命令來檢視執行的Java程式。
c:\>jps 21584 Jps 17828 KotlinCompileDaemon 12284 Launcher 24572 28492 ThreadState
可以看到ThreadSate程式的程式號為28492,接下來,輸入“jstack 28492”來檢視ThreadSate程式棧的資訊,如下所示。
c:\>jstack 28492 2020-02-15 00:27:08 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.202-b08 mixed mode): "DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x000000001ca05000 nid=0x1a4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "BlockedThread-02" #15 prio=5 os_prio=0 tid=0x000000001ca04800 nid=0x6eb0 waiting for monitor entry [0x000000001da4f000] java.lang.Thread.State: BLOCKED (on object monitor) at io.binghe.concurrent.executor.state.BlockedThread.run(BlockedThread.java:28) - waiting to lock <0x0000000780a7e4e8> (a java.lang.Class for io.binghe.concurrent.executor.state.BlockedThread) at java.lang.Thread.run(Thread.java:748) "BlockedThread-01" #14 prio=5 os_prio=0 tid=0x000000001ca01800 nid=0x6e28 waiting on condition [0x000000001d94f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at io.binghe.concurrent.executor.state.WaitingTime.waitSecond(WaitingTime.java:36) at io.binghe.concurrent.executor.state.BlockedThread.run(BlockedThread.java:28) - locked <0x0000000780a7e4e8> (a java.lang.Class for io.binghe.concurrent.executor.state.BlockedThread) at java.lang.Thread.run(Thread.java:748) "WaitingStateThread" #13 prio=5 os_prio=0 tid=0x000000001ca06000 nid=0x6fe4 in Object.wait() [0x000000001d84f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000780a7b488> (a java.lang.Class for io.binghe.concurrent.executor.state.WaitingState) at java.lang.Object.wait(Object.java:502) at io.binghe.concurrent.executor.state.WaitingState.run(WaitingState.java:29) - locked <0x0000000780a7b488> (a java.lang.Class for io.binghe.concurrent.executor.state.WaitingState) at java.lang.Thread.run(Thread.java:748) "WaitingTimeThread" #12 prio=5 os_prio=0 tid=0x000000001c9f8800 nid=0x3858 waiting on condition [0x000000001d74f000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at java.lang.Thread.sleep(Thread.java:340) at java.util.concurrent.TimeUnit.sleep(TimeUnit.java:386) at io.binghe.concurrent.executor.state.WaitingTime.waitSecond(WaitingTime.java:36) at io.binghe.concurrent.executor.state.WaitingTime.run(WaitingTime.java:29) at java.lang.Thread.run(Thread.java:748) "Service Thread" #11 daemon prio=9 os_prio=0 tid=0x000000001c935000 nid=0x6864 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #10 daemon prio=9 os_prio=2 tid=0x000000001c88c800 nid=0x6a28 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x000000001c880000 nid=0x6498 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x000000001c87c000 nid=0x693c waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x000000001c87b800 nid=0x5d00 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x000000001c862000 nid=0x6034 runnable [0x000000001d04e000] java.lang.Thread.State: RUNNABLE at java.net.SocketInputStream.socketRead0(Native Method) at java.net.SocketInputStream.socketRead(SocketInputStream.java:116) at java.net.SocketInputStream.read(SocketInputStream.java:171) at java.net.SocketInputStream.read(SocketInputStream.java:141) at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284) at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326) at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178) - locked <0x0000000780b2fd88> (a java.io.InputStreamReader) at java.io.InputStreamReader.read(InputStreamReader.java:184) at java.io.BufferedReader.fill(BufferedReader.java:161) at java.io.BufferedReader.readLine(BufferedReader.java:324) - locked <0x0000000780b2fd88> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64) "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c788800 nid=0x6794 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001c7e3800 nid=0x3354 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c771000 nid=0x6968 in Object.wait() [0x000000001cd4f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000780908ed0> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x0000000780908ed0> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216) "Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001c770800 nid=0x6590 in Object.wait() [0x000000001cc4f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000780906bf8> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x0000000780906bf8> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) "VM Thread" os_prio=2 tid=0x000000001a979800 nid=0x5c2c runnable "GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00000000033b9000 nid=0x4dc0 runnable "GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00000000033ba800 nid=0x6690 runnable "GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00000000033bc000 nid=0x30b0 runnable "GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00000000033be800 nid=0x6f68 runnable "GC task thread#4 (ParallelGC)" os_prio=0 tid=0x00000000033c1000 nid=0x6478 runnable "GC task thread#5 (ParallelGC)" os_prio=0 tid=0x00000000033c2000 nid=0x4fe4 runnable "GC task thread#6 (ParallelGC)" os_prio=0 tid=0x00000000033c5000 nid=0x584 runnable "GC task thread#7 (ParallelGC)" os_prio=0 tid=0x00000000033c6800 nid=0x6988 runnable "VM Periodic Task Thread" os_prio=2 tid=0x000000001c959800 nid=0x645c waiting on condition JNI global references: 12
由以上輸出的資訊可以看出:名稱為WaitingTimeThread的執行緒處於TIMED_WAITING狀態;名稱為WaitingStateThread的執行緒處於WAITING狀態;名稱為BlockedThread-01的執行緒處於TIMED_WAITING狀態;名稱為BlockedThread-02的執行緒處於BLOCKED狀態。
注意:使用jps結合jstack命令可以分析線上生產環境的Java程式的異常資訊。
也可以直接點選IDEA下圖所示的圖表直接列印出執行緒的堆疊資訊。
輸出的結果資訊與使用“jstack 程式號”命令輸出的資訊基本一致。
本文分享自華為雲社群《一文搞懂執行緒與執行緒池》,原文作者:冰 河。