日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java线程“生产/消费”模型2

發(fā)布時間:2023/12/1 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java线程“生产/消费”模型2 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
/* 資源類 */ class ShareValue {private int total;//判斷對象是否為空private boolean isEmpty=true;//判斷對象是否已滿private boolean isFull=true;public ShareValue(int total) {this.total = total;if(total>0) isEmpty=false;if(total<1000) isFull=false;}/** synchronized 方法控制對類成員變量的訪問:每個類實例對應(yīng)一把鎖,每個 synchronized 方法都必須獲得調(diào)用該方法的類實例的鎖方能執(zhí)行,* 否則所屬線程阻塞,方法一旦執(zhí)行,就獨占該鎖,直到從該方法返回時才將鎖釋放,此后被阻塞的線程方能獲得該鎖,重新進入可執(zhí)行狀態(tài)。* 這種機制確保了同一時刻對于每一個類實例,其所有聲明為 synchronized 的成員函數(shù)中至多只有一個處于可執(zhí)行狀態(tài)* (因為至多只有一個能夠獲得該類實例對應(yīng)的鎖),從而有效避免了類成員變量的訪問沖突(只要所有可能訪問類成員變量的方法均被聲明為 synchronized)。* */synchronized void putValue(int value){//取得當(dāng)前線程名String name=Thread.currentThread().getName();while(isFull){display("Full ! ["+name+"] waites.");try {this.wait();} catch (InterruptedException e) {System.out.println(e);}}total+=value;if(total>1000){isFull=true;}display(name+",put: "+value+",old value: "+(total-value)+",now value"+total);isEmpty=false;//通知所有的等待線程notifyAll();}synchronized int getValue(int value){String name=Thread.currentThread().getName();while(isEmpty || total<value){display("Empty or not enough! ["+name+"] waits."); try {//進入等待狀態(tài)this.wait();} catch (InterruptedException e) {System.out.println(e);}}display(name+" get: "+value+",old value:"+total+",now value :"+(total-value));if(total-value>=0){total-=value;}if(total==0){isEmpty=true;}if(total<1000){isFull=false;}//通知所有等待的線程notifyAll();return value;}int getNowTotal(){return total;}private void display(String string) {System.out.println(string);} }/* 生產(chǎn)者類 */ class Producer extends Thread {// 共享的ShareValue對象ShareValue share;// 要增加的值int value;public Producer(String name, ShareValue share, int value) {super(name);this.share = share;this.value = value;}public void run() {//同步share對象 ,直到當(dāng)前代碼塊運行完畢后,share的對象鎖才會釋放synchronized (share) {try {sleep(100);} catch (InterruptedException e) {System.out.println(e);}share.putValue(value);}} }/*消費者類*/ class Consumer extends Thread{//共享的ShareValue對象ShareValue share;//要減少的值int value;public Consumer(String name,ShareValue share, int value) {super(name);this.share = share;this.value = value;}public void run(){ //同步share對象,直到當(dāng)前代碼運行完畢后,share的對象鎖才會釋放synchronized (share) {try {sleep(100);} catch (InterruptedException e) {System.out.println(e);}share.getValue(value);}} } /* 測試主類 */ public class TestDemo {public static void main(String[] args) {ShareValue share=new ShareValue(0);Producer producer1=new Producer("producer1", share, 100);Consumer consumer=new Consumer("consumer", share, 300);Producer producer2=new Producer("producer2",share,950);Producer producer3=new Producer("producer3",share,50); // Producer producer4=new Producer("producer4",share,50); // Producer producer5=new Producer("producer5",share,50);producer1.start();consumer.start();producer2.start();producer3.start(); // producer4.start(); // producer5.start();}}

總結(jié)

以上是生活随笔為你收集整理的java线程“生产/消费”模型2的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。