美文网首页
阻塞队列的写法

阻塞队列的写法

作者: _Kantin | 来源:发表于2017-09-24 21:12 被阅读37次

基于linkedList的BlockingQueue的书写

    //java中阻塞队列的应用
public class BlockingQueue {
    private List queue = new LinkedList<>();
    private int limit = 10;
    public BlockingQueue(int limit){
        this.limit = limit;
    }
    public synchronized void enqueue(Object item) throws Exception{
        while(this.queue.size() == this.limit){
            wait();
        }
        if(this.queue.size()==0){
            notify();
        }
        this.queue.add(item);
    }
    public synchronized void dequeue(Object item) throws Exception{
        while(this.queue.size() == this.limit){
            notify();
        }
        if(this.queue.size()==0){
            wait();
        }
        this.queue.remove(0);
    }
}

异步消息队列

原始数据类

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

消费者

public class Consumer  implements Runnable{
    private BlockingQueue<PCData> queue;
    private static final int SLEEPTIME = 1000;
    public Consumer(BlockingQueue<PCData> queue){
        this.queue = queue;
    }
    
    @Override
    public void run() {
        System.out.println("start Consumer id :"+Thread.currentThread().getId());
        Random r = new Random();
        try{
            while(true){
                //注意:take()都remove()都是删除元素并返回队列的头,但是take()操作为空不报错
                PCData data = queue.take();
                if(data != null)
                {
                    int re = data.getData() * data.getData();
                    System.out.println("消费者消耗的数值:  "+MessageFormat.format("{0}*{1}={2}", data.getData(),data.getData(),re));
                    Thread.sleep(r.nextInt(SLEEPTIME));
                }
            }
        }catch (InterruptedException e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
}



生产者

public class Producer implements Runnable{
    private volatile boolean isRunning = true;
    private BlockingQueue<PCData> queue;
    private static AtomicInteger count = new AtomicInteger();
     private static final int SLEEPTIME = 1000;
    public Producer( BlockingQueue<PCData> queue){
        this.queue = queue;
    }
    @Override
    public void run() {
        PCData data = null;
        Random random = new Random();
         System.out.println("start producting id:" + Thread.currentThread().getId());
         try {
            while(isRunning){
                Thread.sleep(random.nextInt(SLEEPTIME));
                data = new PCData(count.incrementAndGet());
                System.out.println(data + "加入队列");
                if(!queue.offer(data, 2, TimeUnit.SECONDS)){
                    System.out.println("加入队列失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Thread.currentThread().interrupt();
        }
    }
    public void stop(){
        isRunning = false;
    }
    
}

主测试方法

public class Main {
    public static void main(String[] args) throws Exception {
        BlockingQueue<PCData> queue = new LinkedBlockingDeque<>(10);
        Producer p1 = new Producer(queue);
        Producer p2 = new Producer(queue);
        Producer p3 = new Producer(queue);
        Consumer c1 = new Consumer(queue);
        Consumer c2 = new Consumer(queue);
        Consumer c3 = new Consumer(queue);
        ExecutorService service = Executors.newCachedThreadPool();
        service.execute(p1);
        service.execute(p2);
        service.execute(p3);
        service.execute(c1);
        service.execute(c2);
        service.execute(c3);
        Thread.sleep(10*1000);
        p1.stop();
        p2.stop();
        p3.stop();
        Thread.sleep(3000);
        service.shutdown();
    }
}

相关文章

  • 阻塞队列的写法

    基于linkedList的BlockingQueue的书写 异步消息队列 原始数据类 消费者 生产者 主测试方法

  • 并发编程之并发队列

    常见的并发队列有2种:阻塞队列和非阻塞队列。阻塞队列使用锁实现,非阻塞队列使用CAS非阻塞算法实现。这2种队列都是...

  • 探讨阻塞队列和线程池源码

    阻塞队列 非阻塞队列是一个先进先出的单向队列(Queue),而BlockingQueue阻塞队列实际是非阻塞队列的...

  • 阻塞队列(一)(BlockingQueue)

    阻塞队列概要 阻塞队列与我们平常接触的普通队列(list)最大的不同点,在于阻塞队列支持阻塞添加和阻塞删除方法。 ...

  • Java多线程之阻塞队列

    一基本概念:1:什么叫阻塞队列阻塞队列都是相对于非阻塞队列而言的,非阻塞队列就是队列不会对当前线程产生阻塞;例如当...

  • 18.阻塞队列

    [TOC] 阻塞队列 阻塞队列首先是一种队列的数据结构,阻塞表现在此队列提供了操作数据的阻塞方法:阻塞队列提供了可...

  • 以LinkedBlockingQueue为例浅谈阻塞队列的实现

    目录 阻塞队列简介阻塞队列的定义Java中的阻塞队列 LinkedBlockingQueue单链表定义锁和等待队列...

  • 有关java多线程的文章

    先行概念 阻塞队列和非阻塞队列 阻塞和非阻塞队列的区别概念:https://www.cnblogs.com/min...

  • 线程池

    [TOC] 线程池 1. 并发队列:阻塞队列和非阻塞队列 区别如下: 入队: 非阻塞队列:当队列中满了的时候,放入...

  • 阻塞队列和线程池浅析(深度好文)

    阻塞队列 概念:当阻塞队列为空时,获取(take)操作是阻塞的;当阻塞队列为满时,添加(put)操作是阻塞的。 好...

网友评论

      本文标题:阻塞队列的写法

      本文链接:https://www.haomeiwen.com/subject/neslextx.html