您好,登錄后才能下訂單哦!
這篇文章主要介紹“JAVA多線程使用Lock,Condition實(shí)現(xiàn)A,B,C,D依次執(zhí)行,怎么實(shí)現(xiàn)排它,同步通訊”,在日常操作中,相信很多人在JAVA多線程使用Lock,Condition實(shí)現(xiàn)A,B,C,D依次執(zhí)行,怎么實(shí)現(xiàn)排它,同步通訊問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JAVA多線程使用Lock,Condition實(shí)現(xiàn)A,B,C,D依次執(zhí)行,怎么實(shí)現(xiàn)排它,同步通訊”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
package com.study; import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class BlockingQueueDemo { public static void main(String[] args) { BlockingQueueDemo blockingQueueDemo = new BlockingQueueDemo(); final BlockingQueueClass blockingQueueClass = blockingQueueDemo.new BlockingQueueClass(); Thread thread = new Thread(new Runnable() { @Override public void run() { while (true) { blockingQueueClass.invokeA(); } } }); thread.start(); Thread thread2 = new Thread(new Runnable() { @Override public void run() { while (true) { blockingQueueClass.invokeB(); } } }); thread2.start(); Thread thread3 = new Thread(new Runnable() { @Override public void run() { while (true) { blockingQueueClass.invokeC(); } } }); thread3.start(); Thread thread4 = new Thread(new Runnable() { @Override public void run() { while (true) { blockingQueueClass.invokeD(); } } }); thread4.start(); } class BlockingQueueClass { Lock lock = new ReentrantLock(); Condition conditionA = lock.newCondition(); Condition conditionB = lock.newCondition(); Condition conditionC = lock.newCondition(); Condition conditionD = lock.newCondition(); boolean syncA = true; boolean syncB = false; boolean syncC = false; boolean syncD = false; public void invokeA() { lock.lock(); try { while (!syncA) { try { conditionA.await(); } catch (InterruptedException e) { e.printStackTrace(); } } syncA = false; syncB = true; System.out.println("invoke A...."); conditionB.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void invokeB() { lock.lock(); try { while (!syncB) { try { conditionB.await(); } catch (InterruptedException e) { e.printStackTrace(); } } syncB = false; syncC = true; System.out.println("invoke B...."); conditionC.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void invokeC() { lock.lock(); try { while (!syncC) { try { conditionC.await(); } catch (InterruptedException e) { e.printStackTrace(); } } syncC = false; syncD = true; System.out.println("invoke C...."); conditionD.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } public void invokeD() { lock.lock(); try { while (!syncD) { try { conditionD.await(); } catch (InterruptedException e) { e.printStackTrace(); } } syncD = false; syncA = true; System.out.println("invoke D...."); conditionA.signal(); } catch (Exception e) { e.printStackTrace(); } finally { lock.unlock(); } } } }
到此,關(guān)于“JAVA多線程使用Lock,Condition實(shí)現(xiàn)A,B,C,D依次執(zhí)行,怎么實(shí)現(xiàn)排它,同步通訊”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。