自己实现阻塞队列

照着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 ..0
put success and try to notify
queue`s 0is already remove
try to notify
put ..1
put success and try to notify
put ..2
put success and try to notify
put ..3
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..4
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..5
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..6
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..7
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..8
put success and try to notify
queue is filled  and begin to wite
queue`s 2is already remove
try to notify
put ..9
put success and try to notify
queue`s 2is already remove
try to notify
queue`s 1is already remove
try to notify
queue`s 0is already remove
try to notify
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值