標籤:
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.next10 * - 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 Node11 * - this Node, meaning the predecessor is tail12 * - null, meaning there is no predecessor13 */14 Node<E> prev;15 16 /**17 * One of:18 * - the real successor Node19 * - this Node, meaning the successor is head20 * - null, meaning there is no successor21 */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 deque11 * @throws NoSuchElementException12 * if this deque is empty13 */14 @Override15 public T remove() {16 return super.removeFirst();17 }View Code
阻塞隊列的工作原理:一個線程(生產者)放入任務,另外一個線程(消費者)取出任務
參考連結:http://www.cnblogs.com/qiengo/archive/2012/12/19/2824971.html
Android開源架構:Universal-Image-Loader解析(四)TaskProcess