Java自己实现阻塞队列

阻塞队列:
当队列为空时,获取操作是阻塞的;当队列为满时,添加操作是阻塞的

判断是否满足阻塞条件,并用条件变量阻塞
加锁,同一时刻只有一个线程进行操作(put/get);

package MutileThread;


import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class RResource3{

    Lock lock = new ReentrantLock();
    Condition c1 = lock.newCondition();
    Condition c2 = lock.newCondition();

    private int defaultSize;
    private Queue<Integer>q;
    private List<Integer> outList;
    public RResource3(Queue<Integer>q,List<Integer>outList,int size)
    {
        this.q = q;
        this.outList = outList;
        this.defaultSize = size;
    }

    public List<Integer> getOutList() {
        return outList;
    }

    public void put(int data)
    {
        try {
            lock.lock();
            while (q.size()==defaultSize)
            {
                System.out.println("队列满了");
                c1.await();
            }

            q.offer(data);
            System.out.println(String.format("Thread :%s, put data %d",Thread.currentThread().getName(),data));
            c2.signalAll();

        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }finally {
            lock.unlock();
        }
    }

    public int  get()
    {
        int t=0;
        try {
            lock.lock();
            if (q.size()==0)
            {
                System.out.println("队列为空");
                c2.await();
            }
             t = q.poll();
            System.out.println(String.format("data: %d out",t));
            outList.add(t);
            Collections.sort(outList);
            c1.signalAll();
        }catch (InterruptedException e)
        {
            e.printStackTrace();
        }finally {
            lock.unlock();
            return t;
        }

    }

}

public class RRun3 {

    public  static void main(String[]args) throws  InterruptedException{
        RResource3 rResource3 = new RResource3(new LinkedList<>(), new CopyOnWriteArrayList<>(), 5);
        CountDownLatch countDownLatch = new CountDownLatch(20);
        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                countDownLatch.countDown();
                rResource3.put(i);
            }
        }, "A").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++) {
                countDownLatch.countDown();
                int t = rResource3.get();
            }
        }, "B").start();

        countDownLatch.await();
        for (int l : rResource3.getOutList()) {
            System.out.println(l);
        }

    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值