Use of queue in java

Source: Internet
Author: User

At the same level as list and set, the queue interface inherits the collection interface. The queue List implements the queue interface. The queue interface narrow the access permission to the method of the queue list (that is, when the parameter type in the method is queue, it can only access the method defined by the queue interface, rather than directly accessing the queue list's non-queue method), so that only the appropriate method can be used. Blockingqueue inherits the queue interface.

A queue is a data structure. it has two basic operations: adding an element at the end of the queue and removing an element from the queue header means that the queue manages data in a first-in-first-out manner, if you try to add an element to a full blocking queue or remove an element from an empty blocking queue, it will cause thread blocking. blocking queues is a useful tool for multi-threaded cooperation. The worker thread can regularly store intermediate results in a blocking queue, while other worker thread can fetch intermediate results and modify them in the future. The queue automatically balances the load. If the first thread set is slower than the second, the second thread set will be blocked while waiting for the result. If the first thread set runs fast, it will wait for the second thread set to catch up. The following table shows the operations for blocking queues in jdk1.5:

Add       Add a fuse                   If the queue is full, an IIIegaISlabEepeplian exception is thrown.
Remove  Removes and returns the elements in the queue header.  If the queue is empty, a NoSuchElementException exception is thrown.
Element Returns the element 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, false is returned.
Poll        Removes and returns the elements in the queue header.   If the queue is empty, null is returned.
Peek      Returns the element of the queue header.           If the queue is empty, null is returned.
Put        Add an element                    Blocking if the queue is full
Take       Removes and returns the elements in the queue header. 

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.