生产者与消费者模型

一、商品类

import javax.swing.*;
import java.util.LinkedList;
import java.util.Queue;
public class Goods {
    private String id;
    private  String name;
    private double price;
    public Goods(String id, String name, double price) {
        this.id = id;
        this.name = name;
        this.price = price;
    }
    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id='" + id + '\'' +
                ", name='" + name + '\'' +
                ", price=" + price +
                '}';
    }
}

二、生产者

import java.util.LinkedList;
import java.util.Queue;
import java.util.Random;
import java.util.UUID;
public class producer implements Runnable {
    private final Queue <Goods> queue;
    public producer(Queue <Goods> queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep ( 1000 );
            } catch (InterruptedException e) {
                e.printStackTrace ( );
            }
            synchronized (this.queue) {
                if (queue.size ( ) > 10) {
                    System.out.println ( "容器已满,请加快消费!" );
                    try {
                        this.queue.wait ( );
                    } catch (InterruptedException e) {
                        e.printStackTrace ( );
                    }
                } else {
                    String id =String.valueOf ( UUID.randomUUID ( ) );
                    Goods goods = new Goods ( id, "包子", new Random ( ).nextDouble ( ) );
                    this.queue.add ( goods );
                }
            }
        }
    }
}

三、消费者

import java.util.Queue;
public class Consumer implements Runnable {
    private final Queue <Goods> queue;
    public Consumer(Queue <Goods> queue) {
        this.queue = queue;
    }
    @Override
    public void run() {
        while (true) {
            try {
                Thread.sleep ( 1000 );
            } catch (InterruptedException e) {
                e.printStackTrace ( );
            }
            synchronized (this.queue) {
                if (this.queue.isEmpty ( )) {
                    System.out.println ( Thread.currentThread ( ).getName ( ) + "容器已空,停止消息,加速生产" );
                    this.queue.notify ( );
                } else {
                    Goods goods = this.queue.poll ( );
                    System.out.println ( Thread.currentThread ( ).getName ( ) + "消费商品" + goods );
                }
            }
        }
    }
}

四、运用命令行输入进行测试类测试

import java.util.LinkedList;
import java.util.Queue;
public final class ProducerConsumerLauncher {
    private  ProducerConsumerLauncher(){}
    public static  void run(int producers,int consumers){
        System.out.println("生产者:" + producers + " 消费者:" + consumers);
        Queue<Goods> queue=new LinkedList<> ( );
        Runnable produce=new producer ( queue );
        Runnable consumer=new Consumer ( queue );
        for(int i=0;i<producers;i++){
            new Thread ( produce,"生产者-"+i ).start ();
        }
        for(int i=0;i<consumers;i++){
            new Thread ( consumer,"消费者-"+i ).start ();
        }
    }
}

五、测试

import java.util.LinkedList;
import java.util.Queue;
public class TestProducerConsumer {
    public static void main(String[] args) {
        //方法一:不灵活
       /* Queue<Goods> queue=new LinkedList <> ( );
        Runnable produce=new producer ( queue );
        Runnable consumer=new Consumer ( queue );
        for(int i=0;i<5;i++){
            new Thread ( produce,"生产者-"+i ).start ();
        }
        for(int i=0;i<2;i++){
            new Thread ( consumer,"消费者-"+i ).start ();
        }*/
       //方法二:输命令行参数测试
      int defaultProducers=5;
      int defaultConsumers=5;
      int producers=defaultProducers;
      int consumers=defaultConsumers;
      if(args.length==1){
          producers=Integer.parseInt ( args[0] );
      }
      if(args.length==2){
          producers=Integer.parseInt ( args[0] );
          consumers=Integer.parseInt ( args[1] );
      }
      if(producers<0){
          producers=defaultProducers;
      }
      if(consumers<0){
          consumers=defaultConsumers;
      }
      ProducerConsumerLauncher.run ( producers,consumers );

    }
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值