top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is BlockingQueue in Java and what is its use?

+1 vote
235 views
What is BlockingQueue in Java and what is its use?
posted Mar 14, 2016 by Shivam Kumar Pandey

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Here is a Java BlockingQueue example. The example uses the ArrayBlockingQueue implementation of the BlockingQueue interface.

First, the BlockingQueueExample class which starts a Producer and a Consumer in separate threads. The Producer inserts strings into a shared BlockingQueue, and the Consumer takes them out.

CODE:

public class BlockingQueueExample {    
    public static void main(String[] args) throws Exception {    
        BlockingQueue queue = new ArrayBlockingQueue(1024);
        Producer producer = new Producer(queue);
        Consumer consumer = new Consumer(queue);

        new Thread(producer).start();
        new Thread(consumer).start();

        Thread.sleep(4000);
    }
}
answer Mar 16, 2016 by Shubham Singh
...