Thinking in Java--queue

Source: Internet
Author: User

Run the following code

 PackageContainers//: Containers/queuebehavior.java//compares the behavior of some of the queuesImportjava.util.concurrent.*;Importjava.util.*;Importnet.mindview.util.*; Public  class queuebehavior {    Private Static intCount =Ten;Static<T>voidTest (queue<t> Queue, Generator<t> gen) { for(inti =0; I < count; i++) Queue.offer (Gen.next ()); while(Queue.peek ()! =NULL) System.out.print (Queue.remove () +" ");    System.out.println (); }StaticClass Gen implements generator<string> {string[] s = ("One three four five six seven"+"Eight nine ten"). Split (" ");intI PublicStringNext() {returnS[i++]; }    } Public Static void Main(string[] args) {Test (NewLinkedlist<string> (),NewGen ()); TestNewPriorityqueue<string> (),NewGen ()); TestNewArrayblockingqueue<string> (count),NewGen ()); TestNewConcurrentlinkedqueue<string> (),NewGen ()); TestNewLinkedblockingqueue<string> (),NewGen ()); TestNewPriorityblockingqueue<string> (),NewGen ()); }}/ * * Output:one three four five six seven eight nine ten eight five four * Nine one seven six ten three T     Wo One, three four five six seven eight * Nine ten one, three four five six seven eight nine ten one, three * Four five six seven eight nine ten eight five four nine one seven six ten * three// :~

The program test method, generates a queue through Queue.offer, and then passes the Queue.remove () exception and loses the element.
Output results,

One, three four five six seven eight nine ten
Eight five four nine one seven six ten three
One, three four five six seven eight nine ten
One, three four five six seven eight nine ten
One, three four five six seven eight nine ten
Eight five four nine one seven six ten three

LinkedList:

The list interface is implemented as a link listing. Implements all optional list operations, and allows all elements (including null). In addition to implementing the list interface, the LinkedList class provides a uniform naming method for the 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 double-ended queue.

Priorityqueue:

A queue of unbounded priorities based on the priority heap. The elements of the priority queue are sorted in their natural order, or sorted according to the Comparator provided when the queue is constructed, depending on the construction method used. The priority queue does not allow the use of NULL elements. Priority queues that rely on natural order also do not allow the insertion of objects that cannot be compared (this can cause classcastexception).

Arrayblockingqueue

A bounded blocking queue supported by the array. This queue sorts elements by FIFO (first-in, in-out) principle. The head of the queue is the element that has the longest time in the queue. The tail of the queue is the element that has the shortest time in the queue. The new element is inserted at the end of the queue, and the queue fetch operation starts from the head of the queue to get the element.

Concurrentlinkedqueue

A node-based, link-free, thread-safe queue. This queue sorts the elements according to the FIFO (first-in, in-out) principle. The head of the queue is the longest element in the queue. The tail of the queue is the element with the shortest time in the queue. The new element is inserted at the end of the queue, and the queue get operation obtains the element from the queue head. Concurrentlinkedqueue is an appropriate choice when multiple threads share access to a common collection. This queue does not allow the use of NULL elements.

Linkedblockingqueue

An arbitrarily scoped blocking queue based on the linked node. This queue sorts elements by FIFO (first-in, in-out). The head of the queue is the longest element in the queue. The tail of the queue is the element with the shortest time in the queue. The new element is inserted at the end of the queue, and the queue fetch operation obtains the element at the head of the queue. The throughput of a linked queue is typically higher than an array-based queue, but in most concurrent applications, its predictable performance is low.

Priorityblockingqueue

A unbounded blocking queue that uses the same order rules as the class Priorityqueue and provides a blocking fetch operation. Although this queue is logically unbounded, attempting to perform an add operation will also fail when the resource is exhausted (causing outofmemoryerror). This class does not allow the use of NULL elements. Priority queues that rely on natural order also do not allow the insertion of objects that cannot be compared (this can cause classcastexception to be thrown).

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

Thinking in Java--queue

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.