Thinking in java,thinkinginjava

來源:互聯網
上載者:User

Thinking in java,thinkinginjava

運行下面代碼

package containers;//: containers/QueueBehavior.java// Compares the behavior of some of the queuesimport java.util.concurrent.*;import java.util.*;import net.mindview.util.*;public class QueueBehavior {    private static int count = 10;    static <T> void test(Queue<T> queue, Generator<T> gen) {        for (int i = 0; i < count; i++)            queue.offer(gen.next());        while (queue.peek() != null)            System.out.print(queue.remove() + " ");        System.out.println();    }    static class Gen implements Generator<String> {        String[] s = ("one two three four five six seven " + "eight nine ten")                .split(" ");        int i;        public String next() {            return s[i++];        }    }    public static void main(String[] args) {        test(new LinkedList<String>(), new Gen());        test(new PriorityQueue<String>(), new Gen());        test(new ArrayBlockingQueue<String>(count), new Gen());        test(new ConcurrentLinkedQueue<String>(), new Gen());        test(new LinkedBlockingQueue<String>(), new Gen());        test(new PriorityBlockingQueue<String>(), new Gen());    }} /*     * Output: one two three four five six seven eight nine ten eight five four     * nine one seven six ten three two one two three four five six seven eight     * nine ten one two three four five six seven eight nine ten one two three     * four five six seven eight nine ten eight five four nine one seven six ten     * three two     */// :~

程式test方法,通過queue.offer產生隊列,並再通過queue.remove()異常並輸出頭元素。
輸出結果,

one two three four five six seven eight nine ten
eight five four nine one seven six ten three two
one two three four five six seven eight nine ten
one two three four five six seven eight nine ten
one two three four five six seven eight nine ten
eight five four nine one seven six ten three two

LinkedList:

List 介面的連結清單實現。實現所有可選的列表操作,並且允許所有元素(包括 null)。除了實現 List 介面外,LinkedList 類還為在列表的開頭及結尾 get、remove 和 insert 元素提供了統一的命名方法。這些操作允許將連結清單用作堆棧、隊列或雙端隊列。

PriorityQueue:

一個基於優先順序堆的無界優先順序隊列。優先順序隊列的元素按照其自然順序進行排序,或者根據構造隊列時提供的 Comparator 進行排序,具體取決於所使用的構造方法。優先順序隊列不允許使用 null 元素。依靠自然順序的優先順序隊列還不允許插入不可比較的對象(這樣做可能導致 ClassCastException)。

ArrayBlockingQueue

一個由數組支援的有界阻塞隊列。此隊列按 FIFO(先進先出)原則對元素進行排序。隊列的頭部 是在隊列中存在時間最長的元素。隊列的尾部 是在隊列中存在時間最短的元素。新元素插入到隊列的尾部,隊列擷取操作則是從隊列頭部開始獲得元素。

ConcurrentLinkedQueue

一個基於連結節點的無界安全執行緒隊列。此隊列按照 FIFO(先進先出)原則對元素進行排序。隊列的頭部 是隊列中時間最長的元素。隊列的尾部 是隊列中時間最短的元素。新的元素插入到隊列的尾部,隊列擷取操作從隊列頭部獲得元素。當多個線程共用訪問一個公用 collection 時,ConcurrentLinkedQueue 是一個恰當的選擇。此隊列不允許使用 null 元素。

LinkedBlockingQueue

一個基於已連結節點的、範圍任意的 blocking queue。此隊列按 FIFO(先進先出)排序元素。隊列的頭部 是在隊列中時間最長的元素。隊列的尾部 是在隊列中時間最短的元素。新元素插入到隊列的尾部,並且隊列擷取操作會獲得位於隊列頭部的元素。連結隊列的輸送量通常要高於基於數組的隊列,但是在大多數並發應用程式中,其可預知的效能要低。

PriorityBlockingQueue

一個無界阻塞隊列,它使用與類 PriorityQueue 相同的順序規則,並且提供了阻塞擷取操作。雖然此隊列邏輯上是無界的,但是資源被耗盡時試圖執行 add 操作也將失敗(導致 OutOfMemoryError)。此類不允許使用 null 元素。依賴自然順序的優先順序隊列也不允許插入不可比較的對象(這樣做會導致拋出 ClassCastException)。

更多內容,詳見JDK1.6中文版。http://tool.oschina.net/apidocs/apidoc?api=jdk-zh

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.