Welcome to

Vcode

首页 / Java / 闲来无事系列-生产者消费者模型

闲来无事系列-生产者消费者模型

思路:生产者加入到阻塞队列,如果队列有数据就消费,若阻塞队列满了,则停止生产,消费者消费。

BlockingQueue 阻塞队列 保证了单次只有1个线程在进行操作

数据包:

public class PCData {
    private final int intData;
    public PCData(int d){
        intData = d;
    }
    public PCData(String d){
        intData = Integer.valueOf(d);
    }
    public int getData(){
        return intData;
    }
    @Override
    public String toString(){
        return "data:"+intData;
    }
}


生产者:

package com.plzero.pack1;

import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 生产者
 */
public class Producer implements Runnable{

    private volatile boolean flag = true;
    private BlockingQueue<PCData> blockingQueue;
    private static final int SLEEPTIME = 1000;
    private static AtomicInteger count = new AtomicInteger();// 总数 原子操作


    public Producer(BlockingQueue<PCData> blockingQueue) {
        this.blockingQueue = blockingQueue;
    }

    @Override
    public void run() {
        PCData pcData=null;
        Random r = new Random();
        System.out.println("start producting id:" + Thread.currentThread().getId());
        while(flag){
            try {
                Thread.sleep(r.nextInt(SLEEPTIME));
                pcData = new PCData(count.incrementAndGet());
                System.out.println("生产者已加入队列: "+ String.valueOf(pcData.getData()));
                if (!blockingQueue.offer(pcData,2, TimeUnit.SECONDS)){
                    System.out.println("生产者加入队列失败");
                }

            } catch (InterruptedException e) {
                e.printStackTrace();
                Thread.currentThread().interrupt();
            }

        }

    }

    public void  stop(){
        flag=false;
    }
}


消费者:

package com.plzero.pack1;

import java.text.MessageFormat;
import java.util.Random;
import java.util.concurrent.BlockingQueue;

public class Consumer implements Runnable {
    private BlockingQueue<PCData> blockingQueue;
    private static final int SLEEPTIME = 1000;

    public Consumer(BlockingQueue<PCData> queue) {
        this.blockingQueue = queue;
    }

    @Override
    public void run() {
        System.out.println("start Consumer id :" + Thread.currentThread().getId());
        Random r = new Random();


        try {
            while (true) {
                PCData pcData = blockingQueue.take();
                if (pcData != null) {
                    int re = pcData.getData() * pcData.getData();
                    System.out.println("消费者以消费: "+pcData.getData());
                            //MessageFormat.format("{0}*{1}={2}", pcData.getData(), pcData.getData(), re));

                    Thread.sleep(r.nextInt(SLEEPTIME));
                }
            }

        } catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }


    }
}


本文暂无评论

发表评论

您的电子邮件地址不会被发布。

>> <<