import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; public class BlockingQueueExample { public static void main(String[] args) throws InterruptedException { BlockingQueuequeue = new LinkedBlockingQueue<>(); queue.put("apple"); queue.put("banana"); queue.put("cherry"); System.out.println(queue.poll()); // apple System.out.println(queue.poll()); // banana System.out.println(queue.poll()); // cherry System.out.println(queue.poll()); // null } }
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class BlockingQueueExample { public static void main(String[] args) throws InterruptedException { BlockingQueueThis example shows how to create an ArrayBlockingQueue object and add elements to it using the offer() method. We then retrieve and remove the elements from the queue using the poll() method, printing them to the console. The java.util.concurrent.BlockingQueue poll() method belongs to the java.util.concurrent package library.queue = new ArrayBlockingQueue<>(3); queue.offer(1); queue.offer(2); queue.offer(3); System.out.println(queue.poll()); // 1 System.out.println(queue.poll()); // 2 System.out.println(queue.poll()); // 3 System.out.println(queue.poll()); // null } }