Java多线程-线程通信
通信的方式
要想實現多個線程之間的協同,如:線程執行先后順序、獲取某個線程執行的結果等等。涉及到線程之間的相互通信,分為下面四類:
- 文件共享
- 網絡共享
- 共享變量
- JDK提供的線程協調API
- suspend/resume、wait/notify、park/unpark
文件共享
public class MainTest {public static void main(String[] args) {// 線程1 - 寫入數據new Thread(() -> {try {while (true) {Files.write(Paths.get("test.log"),content = "當前時間" + String.valueOf(System.currentTimeMillis()));Thread.sleep(1000L);}} catch (Exception e) {e.printStackTrace();}}).start();// 線程2 - 讀取數據new Thread(() -> {try {while (true) {Thread.sleep(1000L);byte[] allBytes = Files.readAllBytes(Paths.get("test.log"));System.out.println(new String(allBytes));}} catch (Exception e) {e.printStackTrace();}}).start();} }變量共享
public class MainTest {// 共享變量public static String content = "空";public static void main(String[] args) {// 線程1 - 寫入數據new Thread(() -> {try {while (true) {content = "當前時間" + String.valueOf(System.currentTimeMillis());Thread.sleep(1000L);}} catch (Exception e) {e.printStackTrace();}}).start();// 線程2 - 讀取數據new Thread(() -> {try {while (true) {Thread.sleep(1000L);System.out.println(content);}} catch (Exception e) {e.printStackTrace();}}).start();} }網絡共享
線程協作-JDK API
JDK中對于需要多線程協作完成某一任務的場景,提供了對應API支持。
多線程協作的典型場景是:生產者-消費者模型。(線程阻塞、線程喚醒)
示例:線程1去買包子,沒有包子,則不再執行。線程2生產出包子,通知線程-1繼續執行。
API-被棄用的suspend和resume
作用:調用suspend掛起目標線程,通過resume可以恢復線程執行。
/** 包子店 */ public static Object baozidian = null;/** 正常的suspend/resume */ public void suspendResumeTest() throws Exception {// 啟動線程Thread consumerThread = new Thread(() -> {if (baozidian == null) { // 如果沒包子,則進入等待System.out.println("1、進入等待");Thread.currentThread().suspend();}System.out.println("2、買到包子,回家");});consumerThread.start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();consumerThread.resume();System.out.println("3、通知消費者"); }被棄用的主要原因是,容易寫出不死鎖的代碼。所以用wait/notify和park/unpark機制對它進行替代
suspend和resume死鎖示例
1、同步代碼中使用
/** 死鎖的suspend/resume。 suspend并不會像wait一樣釋放鎖,故此容易寫出死鎖代碼 */public void suspendResumeDeadLockTest() throws Exception {// 啟動線程Thread consumerThread = new Thread(() -> {if (baozidian == null) { // 如果沒包子,則進入等待System.out.println("1、進入等待");// 當前線程拿到鎖,然后掛起synchronized (this) {Thread.currentThread().suspend();}}System.out.println("2、買到包子,回家");});consumerThread.start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();// 爭取到鎖以后,再恢復consumerThreadsynchronized (this) {consumerThread.resume();}System.out.println("3、通知消費者");}2、suspend比resume后執行
/** 導致程序永久掛起的suspend/resume */public void suspendResumeDeadLockTest2() throws Exception {// 啟動線程Thread consumerThread = new Thread(() -> {if (baozidian == null) {System.out.println("1、沒包子,進入等待");try { // 為這個線程加上一點延時Thread.sleep(5000L);} catch (InterruptedException e) {e.printStackTrace();}// 這里的掛起執行在resume后面Thread.currentThread().suspend();}System.out.println("2、買到包子,回家");});consumerThread.start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();consumerThread.resume();System.out.println("3、通知消費者");consumerThread.join();}wait/notify機制
這些方法只能由同一對象鎖的持有者線程調用,也就是寫在同步塊里面,否則會拋出IllegalMonitorStateException異常。
wait方法導致當前線程等待,加入該對象的等待集合中,并且放棄當前持有的對象鎖。
notify/notifyAll方法喚醒一個或所有正在等待這個對象鎖的線程。
注意:雖然會wait自動解鎖,但是對順序有要求,如果在notify被調用之后,才開始wait方法的調用,線程會永遠處于WAITING狀態。
wait/notify代碼示例
/** 正常的wait/notify */public void waitNotifyTest() throws Exception {// 啟動線程new Thread(() -> {synchronized (this) {while (baozidian == null) { // 如果沒包子,則進入等待try {System.out.println("1、進入等待");this.wait();} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println("2、買到包子,回家");}).start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();synchronized (this) {this.notifyAll();System.out.println("3、通知消費者");}}造成死鎖的示例
/** 會導致程序永久等待的wait/notify */public void waitNotifyDeadLockTest() throws Exception {// 啟動線程new Thread(() -> {if (baozidian == null) { // 如果沒包子,則進入等待try {Thread.sleep(5000L);} catch (InterruptedException e1) {e1.printStackTrace();}synchronized (this) {try {System.out.println("1、進入等待");this.wait();} catch (InterruptedException e) {e.printStackTrace();}}}System.out.println("2、買到包子,回家");}).start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();synchronized (this) {this.notifyAll();System.out.println("3、通知消費者");}}park/unpark機制
線程調用park則等待“許可”,unpark方法為指定線程提供“許可(permit)”
不要求park和unpark方法的調用順序。
多次調用unpark之后,再調用park,線程會直接運行。
但不會疊加,也就是說,連續多次調用park方法,第一次會拿到"許可"直接運行,后續調用會進入等待。
造成死鎖的示例
/** 死鎖的park/unpark */public void parkUnparkDeadLockTest() throws Exception {// 啟動線程Thread consumerThread = new Thread(() -> {if (baozidian == null) { // 如果沒包子,則進入等待System.out.println("1、進入等待");// 當前線程拿到鎖,然后掛起synchronized (this) {LockSupport.park();}}System.out.println("2、買到包子,回家");});consumerThread.start();// 3秒之后,生產一個包子Thread.sleep(3000L);baozidian = new Object();// 爭取到鎖以后,再恢復consumerThreadsynchronized (this) {LockSupport.unpark(consumerThread);}System.out.println("3、通知消費者");}偽喚醒
警告!之前代碼中用if語句來判斷,是否進入等待狀態,是錯誤的!
官方建議應該循環中檢查等待條件,原因是處于等待狀態的線程可能會收到錯誤警報和偽喚醒,如果不在循環中檢查等待條件,程序就會在沒有滿足結束條件的情況下退出。
偽喚醒是指線程并非因為notify、notifyall、unpark等api調用而喚醒,是更底層原因導致的。
轉載于:https://www.cnblogs.com/loveyous/p/11456553.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的Java多线程-线程通信的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net core独立发布文件过多的问题
- 下一篇: java access jdbc_Jav