Java Methods of LinkedBlockingQueue

https:‮/‬/www.theitroad.com

Java's LinkedBlockingQueue class provides several methods to manipulate the queue. Here are some of the most commonly used methods:

  1. add(E e): This method adds the specified element to the end of the queue.

  2. offer(E e): This method adds the specified element to the end of the queue. If the queue is full, it returns false.

  3. put(E e): This method adds the specified element to the end of the queue. If the queue is full, the calling thread is blocked until space is available.

  4. poll(): This method removes and returns the first element in the queue, if it is not empty. If the queue is empty, it returns null.

  5. take(): This method removes and returns the first element in the queue, if it is not empty. If the queue is empty, the calling thread is blocked until an element is available.

  6. peek(): This method returns the first element in the queue, if it is not empty. If the queue is empty, it returns null.

  7. size(): This method returns the number of elements in the queue.

  8. contains(Object o): This method returns true if the queue contains the specified element, otherwise false.

  9. toArray(): This method returns an array containing all the elements in the queue, in the order they were added.

  10. iterator(): This method returns an iterator over the elements in the queue.

  11. drainTo(Collection<? super E> c): This method removes all the elements from the queue and adds them to the specified collection c.

These methods provide the basic functionality to manipulate a LinkedBlockingQueue. The choice of which method to use depends on the specific requirements of the application.