栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

main
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;
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/707025.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号