使用BlockQueue實現生產者和消費者模式

lonecloud發表於2018-05-07

 

資料

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);
        }
    }
}

  

相關文章