BlockingQueue in Java
BlockingQueue is an interface in java, which is useful for picking and inserting the elements into the Queue. Which is a thread safe meaning that multiple threads can insert and pick the elements at a time and also BlockingQueue have the capability of blocking threads while inserting and picking the elements from the Queue.
BlockingQueue inserting and picking can be done in four different ways and the following are the list of methods supported by the BlockingQueue.
||offer(e, time, unit)
Any operation in the BlockingQueue can be performed in four ways except the examine operation. Lets what is four operations are there.
add(e) If this operation is not possible immediately then an exception will be thrown.
offer(e) If this operation is not possible immediately then it will return false otherwise true.
put(e) This operation will wait until it gets success in other words it will block the current thread until operation gets success.
offer(e, time, unit) This operation is not possible immediately then it will until the specified time limit for success otherwise it will returns false
remove(Object e) If the queue contains the element e then it will remove that element and return true otherwise return false. If the Object e is not compatible with Queue will lead to an exception
poll() It will retrieve and removes the head of the queue, in case Queue is empty return null.
take() It will retrieve and removes the head of the queue, in case Queue is empty thread will wait until the element becomes available.
poll(time, unit) It will retrieve and removes the head of the queue, in case Queue is empty will wait until the specified time exhausted
element() :-It will retrieve and will not remove the head of the queue, in case Queue is empty will lead to an exception
peek():- It will retrieve and will not remove the head of the queue, in case Queue is empty returns null.
The following are the Classes which are implemented the BlockingQueue