基于線程的wait和notify使用,生產(chǎn)消費案例
多個線程可以相互競爭,也可以互相協(xié)作完成一件事情。
Object的相關方法
Object相關方法 | 描述 |
---|---|
void wait() | 讓當前線程等待,如果沒有被喚醒,就一直等待 |
void wait(long timeout) | 讓當前線程等待指定毫秒值,如果到了指定的毫秒值自動喚醒 |
void notify() | 喚醒一個線程,喚醒的是當前對象鎖下的一個線程 |
void notifyAll() | 喚醒所有線程,喚醒的是當前對象鎖下面的所有線程 |
這些方法一定要放在同步代碼塊中去使用,并且這些方法要通過鎖對象去調(diào)用【***】
案例:
生產(chǎn)方每生產(chǎn)一個產(chǎn)品就需要等待(通知)消費方消費完產(chǎn)品后才能繼續(xù)生產(chǎn)
消費方每消費一個產(chǎn)品就需要等待(通知)生產(chǎn)方去生產(chǎn)產(chǎn)品后才能繼續(xù)消費。
案例圖解
生產(chǎn)方邏輯圖
消費方邏輯圖
代碼實現(xiàn)
【注意】
notify、wait寫在同步代碼塊中,并且使用同一個對象(共有對象:倉庫)進行操作。
this.cangku.wait() 和this.wait() 前一個使用的是倉庫對象 ,后一個使用的是當前任務對象(使用后一個會造成死鎖)
//倉庫 - 唯一(鎖對象,任何對象都可以,用共有對象做鎖對象) class CangKu { //當作 鎖對象 //定義一個變量體現(xiàn)數(shù)量 public int productNum = 0; } //生產(chǎn)方和消費方共用一個倉庫 //生產(chǎn)方 class ProductTask implements Runnable { private CangKu cangKu; //共用一個倉庫不能自己創(chuàng)建,由外部傳入 public ProductTask(CangKu cangKu) { //通過構(gòu)造函數(shù)初始化 this.cangKu = cangKu; } @Override public void run() { while (true) { //通知notify與等待wait必須寫在同步代碼塊中 synchronized (this.cangKu) {//判斷是否有鎖可用,有就進入 if (this.cangKu.productNum == 0) { ++this.cangKu.productNum; //生產(chǎn)數(shù)目+1 System.out.println("生產(chǎn)了一個產(chǎn)品,當前產(chǎn)品數(shù)目:" + this.cangKu.productNum); //通知消費者,必須用同一個鎖對象,不然會造成死鎖 this.cangKu.notify(); } else { //當前還有存貨不用生產(chǎn),等待通知 try { System.out.println("生產(chǎn)方等待中..."); this.cangKu.wait(); } catch (InterruptedException e) { e.printStackTrace(); } }//end if }//end synchronized 出房間釋放鎖 } } } //消費方 class ConsumerTask implements Runnable { private CangKu cangKu; public ConsumerTask(CangKu cangKu) { //構(gòu)造方法 this.cangKu = cangKu; } @Override public void run() { while (true) { synchronized (this.cangKu) { //判斷,倉庫是否為0 if (this.cangKu.productNum == 0) { try { System.out.println("消費方等待中..."); this.cangKu.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } else { //有貨可以吃 -- this.cangKu.productNum ; System.out.println("消費了一個產(chǎn)品,當前產(chǎn)品數(shù)目:" + this.cangKu.productNum); //通知生產(chǎn)方生產(chǎn)產(chǎn)品 this.cangKu.notify(); }//end if }//end synchronized } } } public class Wait_Notify_Demo { public static void main(String[] args) { //任務對象(生產(chǎn)方和消費方共用一個倉庫) CangKu cangKu = new CangKu(); ProductTask productTask = new ProductTask(cangKu); ConsumerTask consumerTask = new ConsumerTask(cangKu); //定義線程(用Executors線程池) ExecutorService pool = Executors.newFixedThreadPool(2); pool.submit(productTask); //生產(chǎn) pool.submit(consumerTask); //消費 } }
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java高并發(fā)系統(tǒng)限流算法的實現(xiàn)
這篇文章主要介紹了Java高并發(fā)系統(tǒng)限流算法的應用,在開發(fā)高并發(fā)系統(tǒng)時有三把利器用來保護系統(tǒng):緩存、降級和限流,限流可以認為服務降級的一種,限流是對系統(tǒng)的一種保護措施,需要的朋友可以參考下2022-05-05Springboot實現(xiàn)頁面間跳轉(zhuǎn)功能
這篇文章主要介紹了Springboot實現(xiàn)頁面間跳轉(zhuǎn)功能,本文給大家分享兩種方式,方法一和方法二是不沖突的,但是通常情況下如果用方法二addViewControllers,需要把方法一所寫的Controller類給注釋掉,需要的朋友可以參考下2023-10-10