Java多線程生產(chǎn)者消費(fèi)者模式實(shí)現(xiàn)過程解析
單生產(chǎn)者與單消費(fèi)者
示例:
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(); } /** * 生產(chǎn)者 */ 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();} } } /** * 消費(fèi)者 */ 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 = ''; }}
執(zhí)行結(jié)果如下:
多生產(chǎn)者與多消費(fèi)者
這種模式下,容易出現(xiàn)“假死”,也就是全部線程都進(jìn)入了 WAITNG 狀態(tài),程序不在執(zhí)行任何業(yè)務(wù)功能了,整個項(xiàng)目呈停止?fàn)顟B(tài)。
示例:
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();} }, '生產(chǎn)者' + (i + 1)); cThread[i] = new Thread(() -> {while (true) { consume.getValue();} }, '消費(fèi)者' + (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('生產(chǎn)者 ' + Thread.currentThread().getName() + ' WAITING了⭐'); lock.wait(); } System.out.println('生產(chǎn)者 ' + 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('消費(fèi)者 ' + Thread.currentThread().getName() + ' WAITING了⭐'); lock.wait(); } System.out.println('消費(fèi)者 ' + Thread.currentThread().getName() + 'RUNNABLE了'); ValueObject.value = ''; lock.notify();} } catch (InterruptedException e) {e.printStackTrace(); } } } static class ValueObject { public static String value = ''; }}
運(yùn)行結(jié)果如圖:
分析:
雖然代碼中通過 wait/notify 進(jìn)行通信了,但是不能保證 notify 喚醒的一定是異類,也可能是同類,比如“生產(chǎn)者”喚醒了“生產(chǎn)者”這樣的情況。
解決方案:
假死出現(xiàn)的主要原因是有可能連續(xù)喚醒了同類。所以解決方案很簡單,就是把 notify() 改為 notifyAll() 即可。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. XML入門的常見問題(三)3. 前端html+css實(shí)現(xiàn)動態(tài)生日快樂代碼4. XML實(shí)體注入深入理解5. Xpath語法格式總結(jié)6. 不要在HTML中濫用div7. 利用CSS3新特性創(chuàng)建透明邊框三角8. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例9. WMLScript腳本程序設(shè)計(jì)第1/9頁10. CSS Hack大全-教你如何區(qū)分出IE6-IE10、FireFox、Chrome、Opera
