The BlockingQueue interface in Java is a thread-safe queue from the java.util.concurrent package used in concurrent programming. It supports blocking operations, making it ideal for producer-consumer scenarios.
Supports blocking methods like put() and take().
Thread-safe, so no explicit synchronization is required.
Does not allow null elements.
Declaration
public interface BlockingQueue<E> extends Queue<E>
Here, E is the type of elements stored in the queue.
The elements of the LinkedBlockingDeque can be accessed using contains(), element(), peek(), poll().
Java
importjava.util.concurrent.*;publicclassGFG{publicstaticvoidmain(String[]args){BlockingQueue<Integer>queue=newLinkedBlockingDeque<>();queue.add(22);queue.add(125);queue.add(723);queue.add(172);queue.add(100);System.out.println("Queue: "+queue);if(queue.contains(22))System.out.println("Queue contains 22");inthead=queue.element();System.out.println("Head of queue: "+head);}}
Output
Queue: [22, 125, 723, 172, 100]
Queue contains 22
Head of queue: 22
3. Deleting Elements
Elements can be deleted from a LinkedBlockingDeque using remove(). Other methods such as take() and poll() can also be used in a way to remove the first and the last elements.
You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.