网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

多线程(二、生产者-消费者模式)

案例介绍

生产者:Producer,消费者Consumer,消费品,Cake,消费品存放队列CakeQueue

代码说明

生产者Producer

public class Producer extends Thread {
    private final CakeQueue cakeQueue;
    private static int cakeNo = 0;
    public Producer(String name, CakeQueue cakeQueue){
        super(name);
        this.cakeQueue = cakeQueue;
    }
    public void run(){
        try {
            while (true) {
                Thread.sleep(1000);
                Cake cake = new Cake(createCakeNo());
                this.cakeQueue.put(cake);
            }
        } catch (InterruptedException e) {
        }
    }
    private static synchronized int createCakeNo() {
        return cakeNo++;
    }

}

消费者Consumer

public class Consumer extends Thread{
    private final CakeQueue cakeQueue;
    public Consumer(String name, CakeQueue cakeQueue) {
        super(name);
        this.cakeQueue = cakeQueue;
    }
    public void run(){
        try {
            while (true) {
                Cake cake = cakeQueue.take();
                Thread.sleep(1000);
            }
        } catch (InterruptedException e) {
        }
    }
}

蛋糕Cake

/**
 * 蛋糕
 */
public class Cake {
    private final int no;
    public Cake(int no) {
        this.no = no;
    }

    public int getNo() {
        return this.no;
    }

    public String toString(){
        return "[ Cake No." + no + "]";
    }

}

队列CakeQueue

/**
 * 存放蛋糕的队列
 */
public class CakeQueue {
    private final Cake[] cakeList;
    private int head;
    private int tail;
    private int counter;

    public CakeQueue(int num) {
        this.cakeList = new Cake[num];
        this.head = 0;
        this.tail = 0;
        this.counter = 0;

    }
    //同步放入一个蛋糕
    public synchronized void put(Cake cake) throws InterruptedException {
        System.out.println(Thread.currentThread().getName() + "put:No" + cake.getNo());
        while(counter >= cakeList.length){
            wait();
        }
        cakeList[tail] = cake;
        tail = (tail + 1)  % cakeList.length;
        counter++;
        notifyAll();

    }
    //同步获取一个蛋糕
    public synchronized Cake take() throws InterruptedException {
        while(counter <= 0){
            wait();
        }
        Cake cake = cakeList[head];
        head = (head + 1) % cakeList.length;
        counter--;
        notifyAll();
        System.out.println("------" + Thread.currentThread().getName() + "take:No" + cake.getNo());
        return cake;
    }

}

启动文件

public class Main {

    public static void main(String[] args) {

        CakeQueue cakeQueue = new CakeQueue(3);
        new Producer("Producer-1:", cakeQueue).start();
        new Producer("Producer-2:", cakeQueue).start();
        new Producer("Producer-3:", cakeQueue).start();
        new Consumer("Consumer-1:", cakeQueue).start();
        new Consumer("Consumer-2:", cakeQueue).start();
        new Consumer("Consumer-3:", cakeQueue).start();

    }
}

运行结果

多线程(二、生产者-消费者模式)

成都创新互联2013年开创至今,先为玉泉街道等服务建站,玉泉街道等地企业,进行企业商务咨询服务。为玉泉街道企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。


分享名称:多线程(二、生产者-消费者模式)
本文网址:http://bjjierui.cn/article/jiojoc.html

其他资讯