Java Methods of ArrayBlockingQueue

www.ig‮.aeditfi‬com

Java's ArrayBlockingQueue 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, if it is not full. If the queue is full, an IllegalStateException is thrown.

  2. offer(E e): This method adds the specified element to the end of the queue, if it is not full. 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 it is not full. 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. remainingCapacity(): This method returns the number of remaining spaces in the queue.

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

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

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

  12. 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 an ArrayBlockingQueue. The choice of which method to use depends on the specific requirements of the application.