多线程学习笔记九——实现有界队列

package day2;

import java.util.LinkedList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

public class BlockingQueue {
    private ReentrantLock lock = new ReentrantLock();
    private Condition notFull = lock.newCondition();
    private Condition notEmpty = lock.newCondition();
    private LinkedList<String> data = new LinkedList<>();
    private int capacity;
    public BlockingQueue(int capacity) {
        this.capacity = capacity;
    }
    public void push(String value) {
        lock.lock();
        try {
            while(data.size()==capacity) {
                try {
                    System.out.println("队列已满,请等待");
                    notFull.await();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            data.addLast(value);
            notEmpty.signal();
        }finally {
            lock.unlock();
        }
    }
    public String get() {
        lock.lock();
        String value=null;
        try {
            while(data.isEmpty()) {
                try {
                    System.out.println("队列为空,请等待");
                    notEmpty.await();
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            value = data.removeFirst();
            notFull.signal();
        }finally {
            lock.unlock();
        }
        return value;
    }
    public static void main(String[] args) {
        BlockingQueue queue = new BlockingQueue(1);
        for(int i = 0;i<5;i++) {
            new Thread() {
                public void run() {
                    System.out.println("取到值"+queue.get());
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
        }

        for(int i = 0;i<5;i++) {
            new Thread() {
                public void run() {
                    queue.push("shidebin");
                    try {
                        TimeUnit.SECONDS.sleep(1);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }.start();
        }
    }
}

console输出:

队列为空,请等待
队列为空,请等待
取到值shidebin
队列为空,请等待
队列为空,请等待
取到值shidebin
队列已满,请等待
取到值shidebin
队列为空,请等待
队列已满,请等待
取到值shidebin
取到值shidebin
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值