《图解java多线程设计模式》 中的 ProducerConsumer 模式

16 篇文章 0 订阅

main

/**
 *生产者消费者模式
 * 生产者安全的将数据交给消费者,但是当消费者和生产者以不同的线程运行时,两者之间的处理速度差异便会引起问题
 * 例如:消费者想要获取数据,可数据还没生成,或者生产者想要交付数据,而消费者的状态嗨无法接受数据等
 * producer-consumer模式在生产者和消费者之间加入了一个“桥梁角色”,该角色用于消除线程间处理速度的差异
 *
 *  Channel通道
 *      为了确保安全性,channel角色会对produce角色和consumer角色的访问进行互斥处理
 *      channel角色而位于produce角色和consumer角色之间承担用于传递data角色的中转站,通道的任务
 *
 */
public class Main {
    public static void main(String[] args) {
        Table table = new Table(3);
        new MakerThread("MakerThread-1",table,31415).start();
        new MakerThread("MakerThread-2",table,92653).start();
        new MakerThread("MakerThread-3",table,58979).start();
        new EnterThread("MakerThread-1",table,32384).start();
        new EnterThread("MakerThread-2",table,62643).start();
        new EnterThread("MakerThread-3",table,38327).start();
    }
}

EnterThread

public class EnterThread extends Thread {
    private final Random random;
    private final Table table;
    public EnterThread(String name,Table table,long seed){
        super(name);
        this.table = table;
        this.random = new Random(seed);
    }
    public void run(){
        try{
            while (true){
                String cake = table.take();
                Thread.sleep(random.nextInt(1000));
            }
        }catch (InterruptedException e){

        }
    }
}

MakerThread

public class MakerThread extends Thread{
    private final Random random;
    private final Table table;
    private static int id = 0;
    public MakerThread(String name,Table table,long seed){
        super(name);
        this.table = table;
        this.random = new Random(seed);
    }
    public void run(){
        try{
            while (true){
                Thread.sleep(random.nextInt(1000));
                String cake = "[Cake No ]" + nextId() + " by " + getName()  + "]";
                table.put(cake);
            }
        } catch (InterruptedException e) {

        }
    }
    public static synchronized int nextId(){
        return id++;
    }
}

Table

public class Table {
    private final String[] buffer;
    private  int tail;      //下次put的位置
    private int head;       //下次take的位置
    private int count;      //buffer中的蛋糕数量

    public Table(int count){
        this.buffer = new String[count];
        this.head = 0;
        this.tail = 0;
        this.count = 0;
    }
    //放置蛋糕
    public synchronized void put(String cake) throws InterruptedException{
        System.out.println(Thread.currentThread().getName() + " puts " + cake);
        while(count >= buffer.length){
            wait();
        }
        buffer[tail] = cake;
        tail = (tail + 1) % buffer.length;      //放置了蛋糕之后,tail就应该前进到下次位置了,基本上只要将tail+1就可以了,但是taile到了buffer的最后一个位置是,必须返回了0所以就进行取余了
        count++;
        notifyAll();
    }
    //拿起蛋糕
    public synchronized String take() throws InterruptedException{
        while(count <= 0){
            wait();
        }
        String cake = buffer[head];
        head = (head + 1) % buffer.length;
        count--;
        notifyAll();
        System.out.println(Thread.currentThread().getName() + " takes " + cake);
        return cake;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值