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