Java生產(chǎn)者和消費者例子_動力節(jié)點Java學(xué)院整理
生產(chǎn)者-消費者(producer-consumer)問題,也稱作有界緩沖區(qū)(bounded-buffer)問題,兩個進程共享一個公共的固定大小的緩沖區(qū)。其中一個是生產(chǎn)者,用于將消息放入緩沖區(qū);另外一個是消費者,用于從緩沖區(qū)中取出消息。問題出現(xiàn)在當緩沖區(qū)已經(jīng)滿了,而此時生產(chǎn)者還想向其中放入一個新的數(shù)據(jù)項的情形,其解決方法是讓生產(chǎn)者此時進行休眠,等待消費者從緩沖區(qū)中取走了一個或者多個數(shù)據(jù)后再去喚醒它。同樣地,當緩沖區(qū)已經(jīng)空了,而消費者還想去取消息,此時也可以讓消費者進行休眠,等待生產(chǎn)者放入一個或者多個數(shù)據(jù)時再喚醒它。
一,首先定義公共資源類,其中的變量number是保存的公共數(shù)據(jù)。
并且定義兩個方法,增加number的值和減少number的值。由于多線程的原因,必須加上synchronized關(guān)鍵字,注意while判斷的條件。
Java代碼
二,分別定義生產(chǎn)
/** * 公共資源類 */ public class PublicResource { private int number = 0; /** * 增加公共資源 */ public synchronized void increace() { while (number != 0) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number++; System.out.println(number); notify(); } /** * 減少公共資源 */ public synchronized void decreace() { while (number == 0) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } number--; System.out.println(number); notify(); } }
者線程和消費者線程,并模擬多次生產(chǎn)和消費,即增加和減少公共資源的number值
Java代碼
/** * 生產(chǎn)者線程,負責生產(chǎn)公共資源 */ public class ProducerThread implements Runnable { private PublicResource resource; public ProducerThread(PublicResource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } resource.increace(); } } } /** * 消費者線程,負責消費公共資源 */ public class ConsumerThread implements Runnable { private PublicResource resource; public ConsumerThread(PublicResource resource) { this.resource = resource; } @Override public void run() { for (int i = 0; i < 10; i++) { try { Thread.sleep((long) (Math.random() * 1000)); } catch (InterruptedException e) { e.printStackTrace(); } resource.decreace(); } } }
三,模擬多個生產(chǎn)者和消費者操作公共資源的情形,結(jié)果須保證是在允許的范圍內(nèi)。
Java代碼
public class ProducerConsumerTest { public static void main(String[] args) { PublicResource resource = new PublicResource(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); new Thread(new ProducerThread(resource)).start(); new Thread(new ConsumerThread(resource)).start(); } }
以上所述是小編給大家介紹的Java生產(chǎn)者和消費者例子,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
elasticsearch索引index之put?mapping的設(shè)置分析
這篇文章主要為大家介紹了elasticsearch索引index之put?mapping的設(shè)置分析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-04-04rabbitmq使用springboot實現(xiàn)direct模式(最新推薦)
這篇文章主要介紹了rabbitmq使用springboot實現(xiàn)direct模式,本文通過示例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-07-07詳解springboot + profile(不同環(huán)境讀取不同配置)
本篇文章主要介紹了springboot + profile(不同環(huán)境讀取不同配置),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-05-05SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作
這篇文章主要介紹了SpringBoot+Mybatis+Vue 實現(xiàn)商品模塊的crud操作,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10淺析Java中print、printf、println的區(qū)別
以下是對Java中print、printf、println的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下2013-08-08