Android开源框架:Universal-Image-Loader解析(四)TaskProcess

Universal-Image-Loader中,对Task的处理有两种方法:FIFO,LIFO

在core/assist下的deque包中,其主要是定义了LIFOLinkedBlockingDeque,其他的几个均在java.util和java.util.concurr中

技术分享

下面我们对queue和deque及其相关的类撸一撸,看看它们的区别

1. Queue:队列, 继承于Collection,定义了几个与队列相关的方法

2. Deque:双向队列,继承于Queue,定义了和双向操作相关的方法

3. BlockingQueue:阻塞队列

4. BlockingDeque:双向阻塞队列

5. AbstractQueue: 实现了队列的几个基本方法,

6. LinkedBlockingQueue:线程安全,阻塞队列,默认长度是Integer.MAX_VALUE

    按 FIFO(先进先出)排序元素。队列的头部 是在队列中时间最长的元素。队列的尾部 是在队列中时间最短的元素。 链接队列的吞吐量通常要高于基于数组的队列。

技术分享
 1 /**
 2      * Linked list node class,单向的列表
 3      */
 4     static class Node<E> {
 5         E item;
 6         /**
 7          * One of:
 8          * - the real successor Node
 9          * - this Node, meaning the successor is head.next
10          * - null, meaning there is no successor (this is the last node)
11          */
12 
13         Node<E> next;
14         Node(E x) { item = x; }
15     }
View Code

 

7. LinkedBlockingDeque:线程安全,双向阻塞队列,其实现主要是基于两个Node,默认长度是Integer.MAX_VALUE

技术分享
 1 /** Doubly-linked list node class 双向链表*/
 2     static final class Node<E> {
 3         /**
 4          * The item, or null if this node has been removed.
 5          */
 6         E item;
 7 
 8         /**
 9          * One of:
10          * - the real predecessor Node
11          * - this Node, meaning the predecessor is tail
12          * - null, meaning there is no predecessor
13          */
14         Node<E> prev;
15 
16         /**
17          * One of:
18          * - the real successor Node
19          * - this Node, meaning the successor is head
20          * - null, meaning there is no successor
21          */
22         Node<E> next;
23 
24         Node(E x) {
25             item = x;
26         }
27     }
28 
29     /**
30      * Pointer to first node.头节点
31      * Invariant: (first == null && last == null) ||
32      *            (first.prev == null && first.item != null)
33      */
34     transient Node<E> first;
35 
36     /**
37      * Pointer to last node.尾节点
38      * Invariant: (first == null && last == null) ||
39      *            (last.next == null && last.item != null)
40      */
41     transient Node<E> last;
View Code

8. LIFOLinkedBlockingDeque:后进先出,双向阻塞队列,仅仅override两个方法

技术分享
 1 @Override
 2     public boolean offer(T e) {
 3         return super.offerFirst(e);
 4     }
 5 
 6     /**
 7      * Retrieves and removes the first element of this deque. This method differs from {@link #pollFirst pollFirst} only
 8      * in that it throws an exception if this deque is empty.
 9      * 
10      * @return the head of this deque
11      * @throws NoSuchElementException
12      *             if this deque is empty
13      */
14     @Override
15     public T remove() {
16         return super.removeFirst();
17     }
View Code

阻塞队列的工作原理:一个线程(生产者)放入任务,另外一个线程(消费者)取出任务

技术分享

上图参考链接:http://www.cnblogs.com/qiengo/archive/2012/12/19/2824971.html

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。