Java ArrayBlockingQueue drainTo() Method

The drainTo() method of ArrayBlockingQueue() class removes all the elements present in this queue and adds them to the provided collection.

Syntax:

Parameters:

c - This is the collection to which elements are transferred.

maxElements - This is the maximum number of elements to be transferred.

Specified By:

The drainTo() method of ArrayBlockingQueue class is specified by drainTo() method in interface BlockingQueue.

Example 1

Output:

Elements int the queue = [0, 1, 2]
Elements left in the queue :[]
Elements drained in the list[0, 1, 2]

Example 2

Output:

Elements int the queue = [0, 1, 2, 3]
Elements left in the queue : [2, 3]
Elements drained in the list : [0, 1]