Donnie

不积跬步无以至千里

BlockingQueue

        import java.util.LinkedList;
        import java.util.concurrent.TimeUnit;
        import java.util.concurrent.atomic.AtomicInteger;

            public class MyBlockingQueue {
    
        //用于装载元素的集合
        private LinkedList<Object> list = new LinkedList<>();
    
        //计数器
        private AtomicInteger count = new AtomicInteger();
    
        //需要定制上下限
        private final int maxSize ;  //使用final需要初始化数据,1.直接赋值,2,或构造方法赋值,否则会编译错误
    
        private final int minSize = 0;
    
        //构造方法
        public MyBlockingQueue(int maxSize) {
            this.maxSize = maxSize;
        }
    
        //初始化一个对象锁
        private final Object lock = new Object();
    
        public void put(Object obj) {
    
            synchronized (lock) {
    
                //如果计数超过最大容量,用锁等待
                while (count.get() == maxSize) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
    
                }
    
                list.add(obj);
                count.incrementAndGet();
    
                lock.notify();
    
            }
    
        }
    
    
        public Object take() {
    
            Object value = null;
            synchronized (lock) {
    
                //如果拿到没有值,就开启锁阻塞等待put放入值
                while (count.get() == minSize) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
                value = list.removeFirst();
                count.decrementAndGet();
                lock.notify();
                System.out.println("移除元素:" + value);
    
            }
    
            return value;
    
        }
    
    
        public int getSize() {
           return this.count.get();
        }
    
        public static void main(String[] args) {
    
            MyBlockingQueue myQueue = new MyBlockingQueue(5);
            myQueue.put("a");
            myQueue.put("b");
            myQueue.put("c");
            myQueue.put("d");
            myQueue.put("e");
            System.out.println(myQueue.getSize());
    
            Thread t1 =new Thread(new Runnable() {
                @Override
                public void run() {
                    myQueue.put("f");
                    myQueue.put("g");
                }
            });
            t1.start();
    
            Thread t2 = new Thread(new Runnable(){
    
                @Override
                public void run(){
                    myQueue.take();
                    myQueue.take();
                }
            });
    
    
            try {
                TimeUnit.SECONDS.sleep(2);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
    
            t2.start();
    
        }
    
    }
赞赏支持