自己实现阻塞队列

来源:互联网 发布:苹果软件发布 编辑:程序博客网 时间:2024/05/17 01:14

照着ArrayBlockingQueue自己实现了一些阻塞队列,其原理就是生产者消费者关系,先实现了功能,一些细节(为什么不用wite,notify 为什么不用lock.lock())暂时没有去学习。

队列代码:

import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.ReentrantLock;/** * Created on 2017/8/31. */public class MyBlockingQueue {    final Object[] items=new Object[3];    final ReentrantLock lock=new ReentrantLock();    private final Condition notFull;    private static int num=-1;    public MyBlockingQueue() {        this.notFull = lock.newCondition();    }    public void put(Object item) throws InterruptedException {//   如果满了则开始阻塞        lock.lockInterruptibly();        try {            while(items.length==num+1){                System.out.println("queue is filled  and begin to wite");                notFull.await();            }            num++;            items[num]=item;            System.out.println("put .."+item);            System.out.println("put success and try to notify");            notFull.signal();        }finally {            lock.unlock();        }    }    public Object take(int index) throws InterruptedException {    if (items.length<index){        System.out.println("index is overflow...index="+index+",length="+items.length);        return null;    }//    为空则阻塞        lock.lockInterruptibly();        try {            while(num==-1){                System.out.println("queue is empty and begin to wite");                notFull.await();            }            Object result=items[num];            items[num]=null;            System.out.println("queue`s "+num+"is already remove");            num--;            System.out.println("try to notify");            notFull.signal();            return result;        }finally {            lock.unlock();        }    }}

测试类代码:

/** * Created on 2017/8/31. */public class Mytest {    public static void main(String[] args) {        final MyBlockingQueue queue=new MyBlockingQueue();       new Thread(new Runnable() {           @Override           public void run() {               for (int i = 0; i <10 ; i++) {                   try {                       queue.put(i);                       Thread.sleep(10);                   } catch (InterruptedException e) {                       e.printStackTrace();                   }               }           }       }).start();       new Thread(new Runnable() {           @Override           public void run() {               for (int i = 0; i <10 ; i++) {                   try {                       queue.take(0);                       Thread.sleep(100);                   } catch (InterruptedException e) {                       e.printStackTrace();                   }               }           }       }).start();    }}

打印结果:

put ..0put success and try to notifyqueue`s 0is already removetry to notifyput ..1put success and try to notifyput ..2put success and try to notifyput ..3put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..4put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..5put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..6put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..7put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..8put success and try to notifyqueue is filled  and begin to witequeue`s 2is already removetry to notifyput ..9put success and try to notifyqueue`s 2is already removetry to notifyqueue`s 1is already removetry to notifyqueue`s 0is already removetry to notify
原创粉丝点击