Java實(shí)現(xiàn)線程通信的案例講解
什么是線程通信、如何實(shí)現(xiàn)?
所謂線程通信就是線程間相互發(fā)送數(shù)據(jù),線程通信通常通過共享一個(gè)數(shù)據(jù)的方式實(shí)現(xiàn)。
線程間會(huì)根據(jù)共享數(shù)據(jù)的情況決定自己該怎么做,以及通知其他線程怎么做。
線程通信常見模型
生產(chǎn)者與消費(fèi)者模型:生產(chǎn)者線程負(fù)責(zé)生產(chǎn)數(shù)據(jù),消費(fèi)者線程負(fù)責(zé)消費(fèi)數(shù)據(jù)。
要求:生產(chǎn)者線程生產(chǎn)完數(shù)據(jù)后,喚醒消費(fèi)者,然后等待自己;消費(fèi)者消費(fèi)完該數(shù)據(jù)后,喚醒生產(chǎn)者,然后等待自己
public class 多線程_5線程通信 extends Thread{ public static void main(String[] args) { //定義線程類,創(chuàng)建一個(gè)共享的賬戶對象 account3 a=new account3("abc",0); //創(chuàng)建兩個(gè)取錢的線程對象 new drawthread3(a,"小明").start(); new drawthread3(a,"小紅").start(); //創(chuàng)建三個(gè)存錢的線程對象 new savethread(a,"存錢罐1號").start(); new savethread(a,"存錢罐2號").start(); new savethread(a,"存錢罐3號").start(); } } //存錢的線程類 class savethread extends Thread{ //接收處理的賬戶對象 private account3 acc; public savethread(account3 acc,String name){ super(name); this.acc=acc; } public void run(){ try { while (true){ //存錢 acc.savemoney(100000); //休眠2秒 Thread.sleep(2000); } } catch (Exception e) { e.printStackTrace(); } } } //取錢的線程類 class drawthread3 extends Thread{ //接收處理的賬戶對象 private account3 acc; public drawthread3(account3 acc,String name){ super(name); this.acc=acc; } public void run(){ try { while (true){ //取錢 acc.drawmoney3(100000); //休眠2秒 Thread.sleep(2000); } } catch (Exception e) { e.printStackTrace(); } } } class account3{ private String cartId; private double money;//賬戶余額 public account3() { } public account3(String cartId, double money) { this.cartId = cartId; this.money = money; } public String getCartId() { return cartId; } public void setCartId(String cartId) { this.cartId = cartId; } public double getMoney() { return money; } public void setMoney(double money) { this.money = money; } public synchronized void savemoney(double money) { //先獲取是誰來存錢,線程名即是人名 String name=Thread.currentThread().getName(); //判斷賬戶是否有錢 try { if(this.money==0){ //沒錢,存錢 this.money+=money; System.out.println(name+"來存錢,存了:"+money+"存錢后余額為:"+this.money); //有錢了 //喚醒所有線程 this.notifyAll(); //鎖對象,讓當(dāng)前線程進(jìn)入等待 this.wait(); }else { //有錢,不存錢 //喚醒所有線程 this.notifyAll(); //鎖對象,讓當(dāng)前線程進(jìn)入等待 this.wait(); } } catch (Exception e) { e.printStackTrace(); } } public synchronized void drawmoney3(double money) { //先獲取是誰來取錢,線程名即是人名 String name=Thread.currentThread().getName(); try { //判斷賬戶是否夠錢 if(this.money>=money){ //有錢,取錢 this.money-=money; System.out.println(name+"來取錢成功,取了:"+money+"余額是:"+this.money); //沒錢了 //喚醒所有線程 this.notifyAll(); //鎖對象,讓當(dāng)前線程進(jìn)入等待 this.wait(); }else{ //余額不足 //喚醒所有線程 this.notifyAll(); //鎖對象,讓當(dāng)前線程進(jìn)入等待 this.wait(); } } catch (Exception e) { e.printStackTrace(); } } }
以上就是Java實(shí)現(xiàn)線程通信的案例講解的詳細(xì)內(nèi)容,更多關(guān)于Java線程通信的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
intellij idea如何將web項(xiàng)目打成war包的實(shí)現(xiàn)
這篇文章主要介紹了intellij idea如何將web項(xiàng)目打成war包的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07Springboot發(fā)送郵件功能的實(shí)現(xiàn)詳解
電子郵件是—種用電子手段提供信息交換的通信方式,是互聯(lián)網(wǎng)應(yīng)用最廣的服務(wù)。本文詳細(xì)為大家介紹了SpringBoot實(shí)現(xiàn)發(fā)送電子郵件功能的示例代碼,需要的可以參考一下2022-09-09Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析
這篇文章主要為大家介紹了Elasticsearch中FST與前綴搜索應(yīng)用實(shí)戰(zhàn)解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08IDEA新建springboot項(xiàng)目時(shí)未生成pom.xml文件的解決操作
這篇文章主要給大家介紹了關(guān)于IDEA新建springboot項(xiàng)目時(shí)未生成pom.xml文件的解決操作方法,文中通過實(shí)例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2023-02-02詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs
這篇文章主要介紹了詳解spring cloud整合Swagger2構(gòu)建RESTful服務(wù)的APIs,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01