生產者--消費者模型

單個生產者消費者模型

​
   class Goods{
    private String goodsName;  //商品名
    private int count;    //商品數量
    public synchronized void set(String goodsName)throws InterruptedException{ //生產方法
        if(this.count>0){
            System.out.println("還有商品,稍等生產");
            wait();
        }
        this.goodsName=goodsName;
        this.count++;
        System.out.println(toString());
        notify();  //喚醒消費者消費
    }
    public synchronized void get()throws InterruptedException{  //消費方法
        if(this.count==0){
            System.out.println("沒有商品");
            wait();
        }
        this.count--;
        System.out.println(toString());
        notify(); //喚醒生產者生產
    }
    public String toString(){
        return "goodsName="+goodsName+",count="+count+"";
    }
}
class Producer implements  Runnable{
    private Goods goods;
    public Producer (Goods goods){
        super();
        this.goods=goods;
    }
    public void run(){
        try{
            this.goods.set("汽車");
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}
class Consumer implements  Runnable{
    private Goods goods;
    public Consumer(Goods goods){
        super();
        this.goods=goods;
    }
    public void run(){
        try{
            this.goods.get();
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}
public class haha{
    public static void main(String[] args) throws InterruptedException{
        Goods goods=new Goods();
        Thread produceThread=new Thread(new Producer(goods),"生產者線程");
        Thread consumerThread=new Thread(new Consumer(goods),"消費者線程");
        produceThread.start();  //啓動生產者線程
        Thread.sleep(1000);
        consumerThread.start();  //啓動消費者線程
    }
}

​

多個生產者消費者模型

import java.util.ArrayList;
import java.util.List;
class Goods{
    private String goodsName;  //商品名
    private int count;    //商品數量
    public synchronized void set(String goodsName)throws InterruptedException{ //生產方法
        while(this.count>0){
            System.out.println("還有商品,稍等生產");
            wait();
        }
        this.goodsName=goodsName;
        this.count++;
        System.out.println(toString());
        notifyAll();  //喚醒全部消費者線程
    }
    public synchronized void get()throws InterruptedException{  //消費方法
        while(this.count==0){
            System.out.println("沒有商品");
            wait();
        }
        this.count--;
        System.out.println(toString());
        notifyAll(); //喚醒全部生產者線程
    }
    public String toString(){
        return "goodsName="+goodsName+",count="+count+"";
    }
}
class Producer implements  Runnable{
    private Goods goods;
    public Producer (Goods goods){
        super();
        this.goods=goods;
    }
    public void run(){
        try{
            this.goods.set("汽車");
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}
class Consumer implements  Runnable{
    private Goods goods;
    public Consumer(Goods goods){
        super();
        this.goods=goods;
    }
    public void run(){
        try{
            this.goods.get();
        }catch(InterruptedException e){
            e.printStackTrace();
        }

    }
}
public class haha{
    public static void main(String[] args) throws InterruptedException{
        Goods goods=new Goods();
       List<Thread> threadList=new ArrayList<>();
       //10個生產者線程
        for(int i=0;i<10;i++){
            Thread produceThread=new Thread(new Producer(goods));
            produceThread.setName("生產者線程"+i);
            threadList.add(produceThread);
        }
        //6個消費者線程
        for(int i=0;i<10;i++){
            Thread consumerThread=new Thread(new Consumer(goods));
            consumerThread.setName("生產者線程"+i);
            threadList.add(consumerThread);
        }
        //啓動所有線程
        for(Thread thread:threadList){
            thread.start();
        }
    }
}

Lock體系的多生產者消費者模型

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
class Goods{
    private int maxCount;
    private String name;
    private int count;
    private Lock lock=new ReentrantLock();
    private Condition producerCondition=lock.newCondition();  //生產者等待隊列
    private Condition consumerCondition=lock.newCondition();  //消費者等待隊列
    public Goods(int maxCount){
        this.maxCount=maxCount;
    }
    public void setGoods(String name){
        lock.lock();
        try {
            while (count == maxCount) {
                System.out.println(Thread.currentThread().getName()+"商品已達上限");
                try {
                    producerCondition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.name=name;
            count++;
            System.out.println(Thread.currentThread().getName()+"生產"+toString());
            //喚醒消費者線程
             consumerCondition.signalAll();
        }
        finally{
            lock.unlock(); //lock無論怎樣都要釋放鎖
        }
    }
  public void  getGoods(){
        lock.lock();
        try{
            while(count==0){
                System.out.println(Thread.currentThread().getName()+"商品賣完了");
                try{
                    consumerCondition.await();
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
            }
            count--;
            System.out.println(Thread.currentThread().getName()+"消費"+toString());
            //喚醒生產者線程
            producerCondition.signalAll();
        }finally{
              lock.unlock();
        }
  }
  public String toString(){
        return  "name="+name+"count="+count;
  }
}
class Producer implements Runnable{
    private Goods goods;

    public  Producer(Goods goods) {
        this.goods = goods;
    }

    public void run(){
        while(true){
            this.goods.setGoods("生產一臺奔馳");
        }
    }
}
class Consuer implements Runnable{
private Goods goods;

public Consuer(Goods goods) {
        this.goods = goods;
        }

public void run(){
        while(true){
        this.goods.getGoods();
        }
   }
}
public class haha{
    public static void main(String[] args) {
 Goods goods=new Goods(50);
 Producer producer=new Producer(goods);
 Consuer consuer=new Consuer(goods);
List<Thread> list=new ArrayList<>();
//5個生產者
        for(int i=0;i<5;i++){
            Thread thread=new Thread(producer,"生產者"+i);
            list.add(thread);
        }
        //10個消費者
        for(int i=0;i<10;i++){
            Thread thread=new Thread(consuer,"消費者"+i);
            list.add(thread);
        }
        for(Thread thread1:list){
            thread1.start();
        }
    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章