使用BlockQueue实现生产者和消费者模式

 

数据

package cn.lonecloud.procum;

/**
 * @author lonecloud
 * @version v1.0
 * @date 上午11:00 2018/5/7
 */
public class Data {

    private String data;

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }
}

  

生产者

package cn.lonecloud.procum;

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

/**
 * @author lonecloud
 * @version v1.0
 * @date 上午10:58 2018/5/7
 */
public class Provider implements Runnable {
  
    private BlockingQueue<Data> queue;

    private static AtomicInteger integer = new AtomicInteger(0);

    public Provider(BlockingQueue<Data> queue) {
        this.queue = queue;
    }

    Random random = new Random();

    @Override
    public void run() {
        while (true) {
            int i = random.nextInt(1000);
            try {
          //模拟数据耗时 Thread.sleep(i); } catch (InterruptedException e) { e.printStackTrace(); } Data data = new Data(); data.setData(String.valueOf(integer.getAndIncrement())); try { if (!queue.offer(data, 20, TimeUnit.SECONDS)) { System.out.println("offer error"); } } catch (InterruptedException e) { e.printStackTrace(); } } } }

 消费者

package cn.lonecloud.procum;

import java.util.Objects;
import java.util.Random;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.TimeUnit;

/**
 * @author lonecloud
 * @version v1.0
 * @date 上午10:58 2018/5/7
 */
public class Customer implements Runnable {
    private BlockingQueue<Data> queue;

    public Customer(BlockingQueue<Data> queue) {
        this.queue = queue;
    }

    Random random = new Random();

    @Override
    public void run() {
        while (true) {
            try {
                Data data = queue.take();
                System.out.println("---------"+data.getData());
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            int i = random.nextInt(1000);
            try {
                Thread.sleep(i);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

 主函数

package cn.lonecloud.procum;

import cn.lonecloud.thread.factory.TraceThreadPool;

import java.util.concurrent.*;

/**
 * @author lonecloud
 * @version v1.0
 * @date 上午11:06 2018/5/7
 */
public class Main {
    public static void main(String[] args) {
        BlockingQueue queue = new ArrayBlockingQueue(10);
//        ThreadPoolExecutor poolExecutor=new TraceThreadPool(5,5,20, TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
        ExecutorService poolExecutor=Executors.newFixedThreadPool(5);
        for (int i = 0; i < 3; i++) {
            Customer customer = new Customer(queue);
            poolExecutor.submit(customer);
        }
        for (int i = 0; i < 5; i++) {
            Provider provider = new Provider(queue);
            poolExecutor.submit(provider);
        }
    }
}

  

posted @ 2018-05-07 14:59  lonecloud  阅读(1745)  评论(0编辑  收藏  举报
我的博客即将同步至 OSCHINA 社区,这是我的 OSCHINA ID:lonecloud,邀请大家一同入驻:https://www.oschina.net/sharing-plan/apply