国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Java多線程生產者消費者模式實現過程解析

瀏覽:76日期:2022-09-04 10:35:50

單生產者與單消費者

示例:

public class ProduceConsume { public static void main(String[] args) { String lock = new String(''); Produce produce = new Produce(lock); Consume consume = new Consume(lock); new Thread(() -> {while (true) { produce.setValue();} }, 'ProductThread').start(); new Thread(() -> {while (true) { consume.getValue();} }, 'ConsumeThread').start(); } /** * 生產者 */ static class Produce { private String lock; public Produce(String lock) {this.lock = lock; } public void setValue() {try { synchronized (lock) { if (!ValueObject.value.equals('')) { lock.wait(); } String value = System.currentTimeMillis() + '_' + System.nanoTime(); System.out.println('set的值是' + value); ValueObject.value = value; lock.notify(); }} catch (InterruptedException e) { e.printStackTrace();} } } /** * 消費者 */ static class Consume { private String lock; public Consume(String lock) {this.lock = lock; } public void getValue() {try { synchronized (lock) { if (ValueObject.value.equals('')) { lock.wait(); } System.out.println('get的值是' + ValueObject.value); ValueObject.value = ''; lock.notify(); }} catch (InterruptedException e) { e.printStackTrace();} } } static class ValueObject { public static String value = ''; }}

執行結果如下:

Java多線程生產者消費者模式實現過程解析

多生產者與多消費者

這種模式下,容易出現“假死”,也就是全部線程都進入了 WAITNG 狀態,程序不在執行任何業務功能了,整個項目呈停止狀態。

示例:

public class MultiProduceConsume { public static void main(String[] args) throws InterruptedException { String lock = new String(''); Produce produce = new Produce(lock); Consume consume = new Consume(lock); Thread[] pThread = new Thread[2]; Thread[] cThread = new Thread[2]; for (int i = 0; i < 2; i++) { pThread[i] = new Thread(() -> {while (true) { produce.setValue();} }, '生產者' + (i + 1)); cThread[i] = new Thread(() -> {while (true) { consume.getValue();} }, '消費者' + (i + 1)); pThread[i].start(); cThread[i].start(); } Thread.sleep(5000); Thread[] threadArray = new Thread[Thread.currentThread().getThreadGroup().activeCount()]; Thread.currentThread().getThreadGroup().enumerate(threadArray); for (int i = 0; i < threadArray.length; i++) { System.out.println(threadArray[i].getName() + ' ' + threadArray[i].getState()); } } static class Produce { private String lock; public Produce(String lock) { this.lock = lock; } public void setValue() { try {synchronized (lock) { while(!ValueObject.value.equals('')) { System.out.println('生產者 ' + Thread.currentThread().getName() + ' WAITING了⭐'); lock.wait(); } System.out.println('生產者 ' + Thread.currentThread().getName() + ' RUNNABLE了'); String value = System.currentTimeMillis() + '_' + System.nanoTime(); ValueObject.value = value; lock.notify();} } catch (InterruptedException e) {e.printStackTrace(); } } } static class Consume { private String lock; public Consume(String lock) { this.lock = lock; } public void getValue() { try {synchronized (lock) { while (ValueObject.value.equals('')) { System.out.println('消費者 ' + Thread.currentThread().getName() + ' WAITING了⭐'); lock.wait(); } System.out.println('消費者 ' + Thread.currentThread().getName() + 'RUNNABLE了'); ValueObject.value = ''; lock.notify();} } catch (InterruptedException e) {e.printStackTrace(); } } } static class ValueObject { public static String value = ''; }}

運行結果如圖:

Java多線程生產者消費者模式實現過程解析

分析:

雖然代碼中通過 wait/notify 進行通信了,但是不能保證 notify 喚醒的一定是異類,也可能是同類,比如“生產者”喚醒了“生產者”這樣的情況。

解決方案:

假死出現的主要原因是有可能連續喚醒了同類。所以解決方案很簡單,就是把 notify() 改為 notifyAll() 即可。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Java
相關文章:
主站蜘蛛池模板: 成人爱爱网站在线观看 | 国产成人精品magnet | 一区二区三区成人 | 色播亚洲精品网站 亚洲第一 | 国产成人3p视频免费观看 | 午夜三级理论在线观看视频 | 国产网友自拍 | 欧美性群另类交 | 国产一区视频在线 | 久久视频免费观看 | 夜色视频一区二区三区 | 成人精品在线视频 | 亚洲小视频网站 | 国产一区二区三区视频 | 成人欧美视频在线观看 | 亚洲综合资源 | a国产片| 成年人网站在线 | 在线视频一二三区 | 国产com| 成人国产在线24小时播放视频 | 九九九九在线视频播放 | 日韩一级a毛片欧美区 | 久久免费视频6 | 女教师的一级毛片 | 九九精品视频在线 | 色综合九九 | 成年人黄色免费网站 | 国产情侣自拍网站 | 亚洲毛片在线免费观看 | 亚洲清纯自偷自拍另类专区 | 久久精品国产亚洲a | 欧美在线视频一区 | 天干夜天天夜天干天ww | 欧美日韩视频一区三区二区 | 人摸人操| 欧洲一级毛片免费 | 久久综合亚洲一区二区三区 | 亚洲精品亚洲人成毛片不卡 | 最新最好看免费毛片基地 | 68久久久久欧美精品观看 |