java concurrent之BlockingQueue

1.接口BlockingQueue

阻塞队列:如果BlockQueue是空的,从BlockingQueue取东西的操作将会被阻断进入等待状态,直到BlockingQueue进了东西才会被唤醒.同样,如果BlockingQueue是满的,任何试图往里存东西的操作也会被阻断进入等待状态,直到BlockingQueue里有空间才会被唤醒继续操作.

2.源码分析(以子类PriorityBlockingQueue为案例作分析)

1)offer(E e):表示如果可能的话,将e加到BlockingQueue里,即如果BlockingQueue可以容纳,则返回true,否则返回false.
2)add(E e):内部重载offer(E e)
3)put(E e):内部重载offer(E e)

4)poll(time):取走BlockingQueue里排在首位的对象,若不能立即取出,则可以等time参数规定的时间,取不到时返回null

5)take():取走BlockingQueue里排在首位的对象,若BlockingQueue为空,阻断进入等待状态直到Blocking有新的对象被加入为止
6)peek(E e):获取但不移除此队列的头;如果此队列为空,则返回 null。

offer();

 public boolean offer(E e) {
        if (e == null)
            throw new NullPointerException();
		//锁保证事务安全	
        final ReentrantLock lock = this.lock;
        lock.lock();
        int n, cap;
        Object[] array;
        while ((n = size) >= (cap = (array = queue).length))
			//底层为数组自增长
            tryGrow(array, cap);
        try {
            Comparator<? super E> cmp = comparator;
            if (cmp == null)
				//根据字典顺序移动底层数组
                siftUpComparable(n, e, array);
            else
				//根据比较器移动底层数组
                siftUpUsingComparator(n, e, array, cmp);
            size = n + 1;
			//释放信号量表示队列不为空;
            notEmpty.signal();
        } finally {
            lock.unlock();
        }
        return true;
    }
poll();

public E poll() {
	//线程安全控制
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
			//出队操作
            return dequeue();
        } finally {
            lock.unlock();
        }
    }


take();

public E take() throws InterruptedException {
        final ReentrantLock lock = this.lock;
        lock.lockInterruptibly();
        E result;
        try {
            while ( (result = dequeue()) == null)
				//空队列保持等待
                notEmpty.await();
        } finally {
            lock.unlock();
        }
        return result;
    }

peek();

public E peek() {
        final ReentrantLock lock = this.lock;
        lock.lock();
        try {
            return (size == 0) ? null : (E) queue[0];
        } finally {
            lock.unlock();
        }
    }  

3.实现子类

所有已知实现类:
ArrayBlockingQueue,DelayQueue, LinkedBlockingDeque, LinkedBlockingQueue, PriorityBlockingQueue, SynchronousQueu
1)ArrayBlockingQueue:规定大小的BlockingQueue,其构造函数必须带一个int参数来指明其大小.其所含的对象是以FIFO(先入先出)顺序排序的.
2)LinkedBlockingQueue:大小不定的BlockingQueue,若其构造函数带一个规定大小的参数,生成的BlockingQueue有大小限制,若不带大小参数,所生成的BlockingQueue的大小由Integer.MAX_VALUE来决定.其所含的对象是以FIFO(先入先出)顺序排序的
3)PriorityBlockingQueue:类似于LinkedBlockQueue,但其所含对象的排序不是FIFO,而是依据对象的自然排序顺序或者是构造函数的Comparator决定的顺序4)SynchronousQueue:特殊的BlockingQueue,对其的操作必须是放和取交替完成的.
ps:LinkedBlockingQueue和ArrayBlockingQueue比较起来,它们背后所用的数据结构不一样,导致LinkedBlockingQueue的数据吞吐量要大于ArrayBlockingQueue,但在线程数量很大时其性能的可预见性低于ArrayBlockingQueue.        

4.其他学习资源

DelayQueue 案例demo : http://www.cnblogs.com/jobs/archive/2007/04/27/730255.html

http://blog.csdn.net/flyingpig4/article/details/6043128

BlockingQueue: http://www.cnblogs.com/likwo/archive/2010/07/01/1769199.html

PriorityBlockingQueue  :http://blog.csdn.net/forever_crying/article/details/8071014




更多多线程精彩内容请继续关注我的博客http://blog.csdn.net/caicongyang

记录与分享,你我共成长 -from  caicongyang






你可能感兴趣的:(BlockingQueue,DelayQueue)