Thinking in java, thinkinginjava

Source: Internet
Author: User

Thinking in java, thinkinginjava

Run the following code:

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     */// :~

Program test method, generate a queue through queue. offer, and then exception through queue. remove () and output the Header element.
Output result,

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

Shortlist:

List interface. Implement all optional list operations and allow all elements (including null ). In addition to the List interface, the revoke List class also provides a unified naming method for get, remove, and insert elements at the beginning and end of the List. These operations allow the link list to be used as a stack, queue, or dual-end queue.

PriorityQueue:

An unbounded priority queue based on the priority heap. The elements of the priority queue are sorted in the natural order, or by the Comparator provided when the queue is constructed, depending on the construction method used. Priority queue does not allow the use of null elements. Non-comparable objects cannot be inserted in priority queues that rely on natural order (this may cause ClassCastException ).

ArrayBlockingQueue

A Bounded blocking queue supported by arrays. This queue sorts the elements according to the FIFO principle. The queue header is the element with the longest time in the queue. The tail of a queue is the element with the shortest time in the queue. Insert the new element to the end of the queue. The queue acquisition operation obtains the element starting from the queue header.

Concurrent1_queue

A connection node-based unbounded thread security queue. This queue sorts the elements according to the FIFO principle. The queue header is the longest element in the queue. The tail of a queue is the minimum time element in the queue. Insert the new element to the end of the queue. The queue acquisition operation obtains the element from the queue header. When multiple threads share access to a public collection, concurrentincluqueue is an appropriate choice. The queue does not allow null elements.

LinkedBlockingQueue

A blocking queue Based on a linked node with any range. This queue sorts elements by FIFO (first-in-first-out. The queue header is the longest element in the queue. The tail of a queue is the element with the shortest time in the queue. The new element is inserted at the end of the queue, and the elements in the queue header are obtained after the queue get operation. The throughput of the link queue is usually higher than that of the array-Based Queue, but its predictable performance is lower in most concurrent applications.

PriorityBlockingQueue

An unbounded blocking queue uses the same sequence rules as the PriorityQueue class and provides blocking obtaining operations. Although this queue is logically unbounded, attempts to perform the add operation will also fail when resources are exhausted (resulting in OutOfMemoryError ). This class does not allow the use of null elements. Priority Queues that depend on natural order cannot insert non-comparable objects (this will cause ClassCastException to be thrown ).

For more information, see the JDK1.6 Chinese version. Http://tool.oschina.net/apidocs/apidoc? Api = jdk-zh

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.