Use of queue in Java

Source: Internet
Author: User

The queue interface is the same level as list and set, and both inherit the collection interface. LinkedList implements the queue interface. The queue interface narrows the access to the LinkedList method (that is, if the parameter type in the method is a queue, it is only possible to access the method defined by the queue interface and not directly to the LinkedList non-queue method). So that only appropriate methods can be used. Blockingqueue inherits the queue interface.

A queue is a data structure. It has two basic operations: adding an element to the tail of the queue, and removing an element from the head of the queue that is, the queue manages the data in a first-in, in-and-out manner if you attempt to add an element to an already full block queue or remove a single cell from an empty blocking queue, causing the thread to block. Blocking queues is a useful tool when working with multiple threads. Worker threads can periodically store intermediate results in a blocking queue while other worker thread threads take intermediate results out and modify them in the future. The queue automatically balances the load. If the first set of threads runs slower than the second, the second line threads blocks when it waits for the result. If the first set of threads runs fast, it waits for the second set of threads to catch up. The following table shows the actions of the blocking queue in jdk1.5:

Add a meta-cable if the queue is full, throw a Iiiegaislabeepeplian exception

Remove removes and returns the element of the head of the queue if the queue is empty, a Nosuchelementexception exception is thrown

Element returns the elements of the queue header if the queue is empty, a Nosuchelementexception exception is thrown

Offer add an element and return true if the queue is full, return false

Poll removing and returning elements to the queue header returns null if the queue is empty

Peek returns the element of the queue header returns null if the queue is empty

Put adds an element block if the queue is full

Take remove and return elements of the queue header if the queue is empty, it blocks

Remove, element, offer, poll, peek are actually belong to the queue interface.

Blocking queues can be divided into three categories based on how they respond: The AAD, Removee, and element operations throw exceptions when you try to add elements to a full queue or to get elements from an empty queue. Of course, in multithreaded programs, the queue can become full or empty at any time, so you might want to use the offer, poll, peek method. These methods only give an error when the task cannot be completed and do not throw an exception.

Note: The poll and Peek methods fail in to return NULL. Therefore, inserting a null value into the queue is not legal.

Use of queue in Java

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.