1
Sponsored Links


Ad by Google
In my previous post, I have posted about LinkedBlockingQueue in java. And in this tutorial, I will try to explain you about SynchronousQueue. After completion of this tutorial, you will be able to answer these questions What is SynchronousQueue? How to use SynchronousQueue and able to solve a producer consumer problem using SynchronousQueue.
What is SynchronousQueue?
The SynchronousQueue is an implementation of BlockingQueue interface, which extends Queue and Queue extends Collection interface.
The SynchronousQueue is totally different implementation of BlockingQueue unlike other implementations of BlockingQueue.
The SynchronousQueue was introduced in java 1.5 under java.util.concurrency package, it is a special queue, it has no capacity to hold any element into the queue, even not a single element, calling peek() method will return null, because it has no capacity to hold any element into the queue. 

The queue where each insert operation must wait for a corresponding remove operation by another thread. That means if you try to insert an element into the queue, it will only insert into the queue if another thread is ready for consumed that element.
As above said no capacity to hold any element into the queue, that means, you cannot iterate as there is no element into the queue, you cannot call other methods of Collection because for Collection SynchornousQueue is an empty queue. The SynchronousQueue does not allow null insertion like other BlockingQueue.
The SynchronousQueue can be used in which an object is running in a thread must sync with an object running in another thread in order to hand it some task,event etc.

Let's solve the popular concurrency problem called Producer Consumer using SynchronousQueue where one thread is producing and another thread is consuming that.
Producer.java Produces messages.
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Chef class to produce pizza
 * 
 * @author subodh.ray
 * 
 */
public class Producer implements Runnable {

 private final BlockingQueue<String> bQueue;
 private final String message;

 public Producer(BlockingQueue<String> bQueue, String message) {
  this.bQueue = bQueue;
  this.message = message;
 }
 @Override
 public void run() {
  try {
   bQueue.put(message);
  } catch (InterruptedException e) {
   e.printStackTrace();
  }
 }

}
Consumer.java Consumed messages produced by Producer.
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;

/**
 * Customer class to consumed pizza
 * 
 * @author subodh.ray
 * 
 */
public class Consumer implements Runnable {

 private BlockingQueue<String> bQueue;

 public Consumer(BlockingQueue<String> bQueue) {
  this.bQueue = bQueue;
 }
 @Override
 public void run() {

  try {
   System.out.println("Consumed - " + bQueue.take());
  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

ProducerConsumerTest.java class where two different thread is running one for producing message and one for consuming that message.
package com.javamakeuse.poc;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.SynchronousQueue;

/**
 * Restaurant class where pizza produce and consumed
 * 
 * @author subodh.ray
 * 
 */
public class ProducerConsumerTest {

 public static void main(String[] args) throws InterruptedException {

  BlockingQueue<String> bQueue = new SynchronousQueue<String>();

  // producing in queue
  (new Thread(new Producer(bQueue, "Large Pizza"))).start();

  // Consuming from the queue
  (new Thread(new Consumer(bQueue))).start();
 }

}
OUT PUT:
Consumed - Large Pizza

If you call peek() method in Consumer.java class instead of take() it will simply return null because element in the SynchronousQueue will inserted only if consume/remove operation is ready by another thread. Here is a solution of Producer Consumer Problem using ArrayBlockingQueue.

Friends, If you liked it or anything to add on this tutorial, you are always welcome you can share your input through comments.
Sponsored Links

1 comments:

  1. If you put an element in SynchronousQueue using put() method it will wait for another thread to receive it, you can't put any other element in the SynchronousQueue as it is blocked.
    SynchronousQueue in Java

    ReplyDelete