Java synchronized線程交替運(yùn)行實(shí)現(xiàn)過(guò)程詳解
背景
用兩個(gè)線程交替輸出A-Z和1-26,即一個(gè)線程輸出A-Z,另一個(gè)線程輸出1-26
而且是交替形式
線程1輸出A——線程二輸出1 線程1輸出B——線程二輸出2 線程1輸出C——線程二輸出3以此類推
分析
主要考察線程之間的通信,思路就是創(chuàng)建兩個(gè)線程
在一個(gè)線程輸出一個(gè)內(nèi)容之后,自己進(jìn)入阻塞,去喚醒另一個(gè)線程
另一個(gè)線程同樣,輸出一個(gè)內(nèi)容之后,自己進(jìn)入阻塞,去喚醒另一個(gè)線程
代碼實(shí)現(xiàn)(一)
public class AlternateCover { public static void main(String[] args) { final char[] arrLetter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.toCharArray(); final String[] arrNumber = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26'}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object();// 設(shè)置一個(gè)鎖對(duì)象 // print arrNumber new Thread(() -> { synchronized (lock) {for (String a : arrNumber) { System.out.print( a); try { lock.notify();// 喚醒其他等待的線程 此處喚醒 arrLetter lock.wait();// arrNumber自己進(jìn)入等待 讓出CPU資源和鎖資源 } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify(); } }, 'arrNumber ').start(); // print arrLetter new Thread(() -> { synchronized (lock) {// 獲取對(duì)象鎖for (char a : arrLetter) { System.out.print(a); try { lock.notify();// 喚醒其他等待的線程 此處喚醒 arrNumber lock.wait();// arrLetter自己進(jìn)入等待 讓出CPU資源和鎖資源 } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify();// 最后那個(gè)等待的線程需要被喚醒,否則程序無(wú)法結(jié)束 } }, 'arrLetter ').start(); }}
運(yùn)行一下,確實(shí)實(shí)現(xiàn)了交替輸出,但是多運(yùn)行幾次,就會(huì)發(fā)現(xiàn)問(wèn)題
有時(shí)候是數(shù)字先輸出,有時(shí)候是字母先輸出
即兩個(gè)線程誰(shuí)先啟動(dòng)的順序是不固定的
倘若試題中再加一句,必須要字母先輸出,怎么辦?
代碼實(shí)現(xiàn)(二)
/** * 交替掩護(hù) 必須保證大寫字母先輸出 */public class AlternateCover { public static volatile Boolean flg = false;// 誰(shuí)先開(kāi)始的標(biāo)志 volatile修飾目的是讓該值修改對(duì)所有線程可見(jiàn),且防止指令重排序 public static void main(String[] args) { final char[] arrLetter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.toCharArray(); final String[] arrNumber = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26'}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object();// 鎖對(duì)象 // print arrLetter new Thread(() -> { synchronized (lock) {if (!flg){ // 如果flg是false 就將值設(shè)為true flg = true;}for (char a : arrLetter) { System.out.print(a);// 輸出內(nèi)容 try { lock.notify();// 喚醒在等待的其他線程中的一個(gè)(此處也只有另一個(gè)) lock.wait();// 自己進(jìn)入等待 讓出CPU資源和鎖資源 } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify();// 最后那個(gè)等待的線程需要被喚醒,否則程序無(wú)法結(jié)束 } }, 'arrLetter').start(); // print arrNumber new Thread(() -> { synchronized (lock) {if (!flg){// 倘若是該線程先執(zhí)行,那么flg次數(shù)還是false 就先等著 try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); }}for (String a : arrNumber) { System.out.print( a); try { lock.notify(); lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify(); } }, 'arrNumber').start(); }}
如此問(wèn)題可以得到解決,但有更優(yōu)(裝)雅(B)的解決辦法
CountDownLatch實(shí)現(xiàn)
/** * 交替掩護(hù) 必須保證大寫字母先輸出 */public class AlternateCover { private static CountDownLatch count = new CountDownLatch(1);// 計(jì)數(shù)器容量為1 public static void main(String[] args) { final char[] arrLetter = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.toCharArray(); final String[] arrNumber = {'1','2','3','4','5','6','7','8','9','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26'}; threadRun(arrLetter, arrNumber); } private static void threadRun(char[] arrLetter,String[] arrNumber){ final Object lock = new Object(); // print arrLetter new Thread(() -> { synchronized (lock) {// 獲取對(duì)象鎖count.countDown();// 對(duì)計(jì)數(shù)器進(jìn)行遞減1操作,當(dāng)計(jì)數(shù)器遞減至0時(shí),當(dāng)前線程會(huì)去喚醒阻塞隊(duì)列里的所有線程(只針對(duì)count)for (char a : arrLetter) { System.out.print(a); try { lock.notify();// 喚醒其他等待的線程 此處喚醒 arrNumber lock.wait();// arrLetter自己進(jìn)入等待 讓出CPU資源和鎖資源 } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify();// 最后那個(gè)等待的線程需要被喚醒,否則程序無(wú)法結(jié)束 } }, 'arrLetter ').start(); // print arrNumber new Thread(() -> { synchronized (lock) {try { count.await();// 如果該線程先執(zhí)行 阻塞當(dāng)前線程,將當(dāng)前線程加入阻塞隊(duì)列} catch (InterruptedException e) { e.printStackTrace();}for (String a : arrNumber) { System.out.print( a); try { lock.notify();// 喚醒其他等待的線程 此處喚醒 arrLetter lock.wait();// arrNumber自己進(jìn)入等待 讓出CPU資源和鎖資源 } catch (InterruptedException e) { e.printStackTrace(); }}lock.notify(); } }, 'arrNumber ').start(); }}
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Python調(diào)用接口合并Excel表代碼實(shí)例2. .net如何優(yōu)雅的使用EFCore實(shí)例詳解3. Python快速將ppt制作成配音視頻課件的操作方法4. ASP.Net Core對(duì)USB攝像頭進(jìn)行截圖5. 一文透徹詳解.NET框架類型系統(tǒng)設(shè)計(jì)要點(diǎn)6. 通過(guò)CSS數(shù)學(xué)函數(shù)實(shí)現(xiàn)動(dòng)畫特效7. ASP.NET MVC使用Boostrap實(shí)現(xiàn)產(chǎn)品展示、查詢、排序、分頁(yè)8. 通過(guò)Ajax方式綁定select選項(xiàng)數(shù)據(jù)的實(shí)例9. ASP.NET MVC實(shí)現(xiàn)橫向展示購(gòu)物車10. ajax動(dòng)態(tài)加載json數(shù)據(jù)并詳細(xì)解析
