您好,登錄后才能下訂單哦!
這篇文章主要介紹“JAVA多線程使用Lock,Condition實現A,B,C,D依次執行,怎么實現排它,同步通訊”,在日常操作中,相信很多人在JAVA多線程使用Lock,Condition實現A,B,C,D依次執行,怎么實現排它,同步通訊問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JAVA多線程使用Lock,Condition實現A,B,C,D依次執行,怎么實現排它,同步通訊”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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(); } } } }
到此,關于“JAVA多線程使用Lock,Condition實現A,B,C,D依次執行,怎么實現排它,同步通訊”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。