這篇文章主要介紹“JAVA多線程使用Lock,Condition實現(xiàn)A,B,C,D依次執(zhí)行,怎么實現(xiàn)排它,同步通訊”,在日常操作中,相信很多人在JAVA多線程使用Lock,Condition實現(xiàn)A,B,C,D依次執(zhí)行,怎么實現(xiàn)排它,同步通訊問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”JAVA多線程使用Lock,Condition實現(xiàn)A,B,C,D依次執(zhí)行,怎么實現(xiàn)排它,同步通訊”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
成都創(chuàng)新互聯(lián)公司是專業(yè)的吳堡網(wǎng)站建設公司,吳堡接單;提供網(wǎng)站設計、網(wǎng)站建設,網(wǎng)頁設計,網(wǎng)站設計,建網(wǎng)站,PHP網(wǎng)站建設等專業(yè)做網(wǎng)站服務;采用PHP框架,可快速的進行吳堡網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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實現(xiàn)A,B,C,D依次執(zhí)行,怎么實現(xiàn)排它,同步通訊”的學習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續(xù)學習更多相關知識,請繼續(xù)關注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
當前文章:JAVA多線程使用Lock,Condition實現(xiàn)A,B,C,D依次執(zhí)行,怎么實現(xiàn)排它,同步通訊
當前網(wǎng)址:http://weahome.cn/article/ppgeii.html