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.