多线程之阻塞式队列

  • 循环队列
public class MyBlockQueue<T> {
    /**
     * 数组模拟循环队列
     */
    private static Object[] queue;
    /**
     * 入队索引
     */
    private static int putIndex;

    /**
     * 出队索引
     */
    private static int takeIndex;

    /**
     * 队列大小
     */
    private static int size;

    public MyBlockQueue(int len){
        queue=new Object[len];
    }
    /**
     * 入队操作
     */
    private void put(T e){
        if(queue.length==size){
            new RuntimeException("队列已满");
        }
        queue[putIndex]=e;
        putIndex=(putIndex+1)%queue.length;
        size++;
    }

    /**
     * 出队
     */
    private T take(){
        if (size==0){
            new RuntimeException("队列空了");
        }

        T t=(T)queue[takeIndex];
        queue[takeIndex]=null;
        takeIndex=(takeIndex+1)%queue.length;
        size--;
        return t;
    }

    /**
     * 队列大小
     * @return
     */
    private int size(){
        return size;
    }

    public static void main(String[] args) {
        MyBlockQueue<Integer> myBlockQueue=new MyBlockQueue(4);

        myBlockQueue.put(1);
        myBlockQueue.put(2);
        myBlockQueue.put(3);
        myBlockQueue.put(4);
        for (Object x:queue){
            System.out.println((Integer)x);
        }
        System.out.println("------------------------------------------");
        myBlockQueue.take();
        for (Object x:queue){
            System.out.println((Integer)x);
        }
        System.out.println("------------------------------------------");
        myBlockQueue.put(5);

        for (Object x:queue){
            System.out.println((Integer)x);
        }
    }
}

在这里插入图片描述

  • 阻塞式队列
/**

 * 实现阻塞队列:
 * 1. 线程安全问题:在多线程下,put、take不具有原子性,4个属性,不具有可见性
 * 2. put操作,如果存满了,需要阻塞等待。take如果是空,需要阻塞等待
 */
public class MyBlockQueue<T> {
    /**
     * 数组模拟循环队列
     */
    private Object[] queue;
    /**
     * 入队索引
     */
    private int putIndex;

    /**
     * 出队索引
     */
    private int takeIndex;

    /**
     * 队列大小
     */
    private  int size;

    public MyBlockQueue(int len){
        queue=new Object[len];
    }
    /**
     * 入队操作
     */
    private synchronized void put(T e) throws InterruptedException {
        //下次入队还可能队满
       while (queue.length==size){
            this.wait();
        }
        queue[putIndex]=e;
        putIndex=(putIndex+1)%queue.length;
        size++;
        this.notifyAll();
    }

    /**
     * 出队
     */
    private synchronized T take() throws InterruptedException {
        //下次竞争还可能是空队列
        while (size==0){
            wait();
        }

        T t=(T)queue[takeIndex];
        queue[takeIndex]=null;
        takeIndex=(takeIndex+1)%queue.length;
        size--;
        notifyAll();
        return t;
    }

    /**
     * 队列大小
     * @return
     */
    private synchronized int size(){
        return size;
    }

    public static void main(String[] args) {
        MyBlockQueue<Integer> myBlockQueue=new MyBlockQueue(4);
        for (int i=0;i<3;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (int j=0;j<10;j++){
                            myBlockQueue.put(j);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            }).start();
        }

        for (int i=0;i<3;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        for (; ; ){
                            int j = myBlockQueue.take();
                            System.out.println(Thread.currentThread().getName() + ":" + j);
                        }
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值