Java線程通信中關于生產者與消費者案例分析
相關方法:
wait():一旦執(zhí)行此方法,當前線程就進入阻塞狀態(tài),并釋放同步監(jiān)視器。
notify():一旦執(zhí)行此方法,就會喚醒被wait的一個線程,如果有多個線程被wait,就喚醒優(yōu)先級高的那個。
notifyAll():一旦執(zhí)行此方法,就會喚醒所有被wait的線程。
說明:
1.wait(),notify(),notifyAll()三個方法必須使用在同步代碼塊或同步方法中。
2.wait(),notify(),notifyAll()三個方法的調用者必須是同步代碼塊或同步方法中的同步監(jiān)視器。
否則,會出現(xiàn)IllegalMonitorStateException異常
3.wait(),notify(),notifyAll()三個方法是定義在java.lang.Object類中。
線程通信的例子:使用兩個線程打印1-100.線程1,線程2 交替打印
class Number implements Runnable{ private int number = 1; @Override public void run() { while(true){ synchronized (this) { notify(); if(number <= 100){ try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getName() + ":" + number); number++; try { //使得調用如下wait()方法的線程進入阻塞狀態(tài) wait(); } catch (InterruptedException e) { e.printStackTrace(); } }else{ break; } } } } } public class CommunicationTest { public static void main(String[] args) { Number number = new Number(); Thread t1 = new Thread(number); Thread t2 = new Thread(number); t1.setName("線程1"); t2.setName("線程2"); t1.start(); t2.start(); } }
經典例題:生產者/消費者問題
生產者(Productor)將產品交給店員(Clerk),而消費者(Customer)從店員處取走產品店員一次只能持有固定數(shù)量的產品(比如:20),如果生產者試圖生產更多的產品,店員會叫生產者停一下,如果店中有空位放產品了再通知生產者繼續(xù)生產,如果店中沒有產品了,店員會告訴消費者等一下,如果店中有產品了再通知消費者來取走產品。
這里可能出現(xiàn)兩個問題:
>生產者比消費者快時,消費者會漏掉一些數(shù)據(jù)沒有取到。
>消費者比生產者塊時,消費者會取相同的數(shù)據(jù)。
分析:
- 是否是多線程問題?是,生產者線程,消費者線程
- 是否有共享數(shù)據(jù)?有,店員(或產品)
- 如何解決線程安全問題?同步機制,有三種方法
- 是否涉及線程的通信?是
class Clerk{ private int productCount = 0; //生產產品 public synchronized void produceProduct() { if(productCount < 20){ productCount++; System.out.println(Thread.currentThread().getName() + ":開始生產第" + productCount + "個產品"); notify(); }else{ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } //消費產品 public synchronized void consumeProduct() { if(productCount > 0){ System.out.println(Thread.currentThread().getName() + ":開始消費第" + productCount + "個產品"); productCount--; notify(); }else{ try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Producer extends Thread{//生產者 private Clerk clerk; public Producer(Clerk clerk){ this.clerk = clerk; } @Override public void run() { System.out.println(getName() + ":開始生產產品....."); while(true){ try { sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } clerk.produceProduct(); } } } class Consumer extends Thread{//消費者 private Clerk clerk; public Consumer(Clerk clerk){ this.clerk = clerk; } @Override public void run() { System.out.println(getName() + ":開始消費產品....."); while(true){ try { sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } clerk.consumeProduct(); } } } public class ProductTest { public static void main(String[] args) { Clerk clerk = new Clerk(); Producer p1 = new Producer(clerk); p1.setName("生產者1"); Consumer c1 = new Consumer(clerk); c1.setName("消費者1"); Consumer c2 = new Consumer(clerk); c2.setName("消費者2"); p1.start(); c1.start(); c2.start(); } }
sleep()和wait()的異同?
1.相同點:一旦執(zhí)行方法,都可以使得當前的線程進入阻塞狀態(tài)。
2.不同點:
1)兩個方法聲明的位置不同,Thread類中聲明sleep(),Object類中聲明wait()
2)調用的要求不同:sleep()可以在任何需要的場景下調用。wait()必須使用在同步代碼塊或同步方 法中
3)關于是否釋放同步監(jiān)視器:如果兩個方法都使用在同步代碼塊或同步方法中,sleep()不會釋放 鎖,wait()會釋放鎖
到此這篇關于Java線程通信中關于生產者與消費者案例分析的文章就介紹到這了,更多相關Java線程通信內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
springboot后端接收前端傳數(shù)組參數(shù)三種方法
這篇文章主要給大家介紹了關于springboot后端接收前端傳數(shù)組參數(shù)三種方法,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2023-07-07Java數(shù)據(jù)結構與算法之棧(動力節(jié)點Java學院整理)
這篇文章主要介紹了Java數(shù)據(jù)結構與算法之棧,棧是先進后出的數(shù)據(jù)的結構,本文通過文字說明與實例代碼相結合的形式給大家介紹的非常詳細,需要的朋友跟著小編一起學習吧2017-04-04Mybatis-Plus實現(xiàn)公共字段自動賦值的方法
這篇文章主要介紹了Mybatis-Plus實現(xiàn)公共字段自動賦值的方法,涉及到通用字段自動填充的最佳實踐總結,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-07-07springboot與mybatis整合實例詳解(完美融合)
大家都知道springboot搭建一個spring框架只需要秒秒鐘。下面通過實例代碼給大家介紹一下springboot與mybatis的完美融合,非常不錯,具有參考借鑒價值,感興趣的朋友一起看看吧2016-09-09spring boot整合mybatis+mybatis-plus的示例代碼
這篇文章主要介紹了spring boot整合mybatis+mybatis-plus的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01