Home » Core Java » util » concurrent » SynchronousQueue » Java.util.concurrent.SynchronousQueue Example

About Anirudh Bhatnagar

Anirudh Bhatnagar
Anirudh is a Java programmer with extensive experience in building Java/J2EE applications. He has always been fascinated by the new technologies and emerging trends in software development. He has been involved in propagating these changes and new technologies in his projects. He is an avid blogger and agile enthusiast who believes in writing clean and well tested code.

Java.util.concurrent.SynchronousQueue Example

In this example we will see how to use Java.util.concurrent.SynchronousQueue. SynchronousQueue is a type of Blocking Queue (it implements BlockingQueue)

As mentioned in the java docs , it is a blocking queue in which each insert operation must wait for a corresponding remove operation by another thread, and vice versa. This means that the queue can only contain a single element internally. A thread inserting an element into the queue will be blocked until another thread takes that element from the queue. Similarly, if a thread tries to take an element and no element is present, that thread will be blocked until a thread inserts an element into the queue.

Lets see this working in an example. In the example we will have a producer thread and a consumer thread.

JavaSynchronousQueueExample.java

package com.javacodegeeks.example;

import java.util.concurrent.SynchronousQueue;

/**
 * 
 * @author anirudh
 *
 */
public class JavaSynchronousQueueExample {

	public static void main(String args[]) {
		final SynchronousQueue queue = new SynchronousQueue();
		
		// start publisher thread
		new Thread(new QueueProducer(queue)).start();
		
		// start consumer thread
		new Thread(new QueueConsumer(queue)).start();
	
	}

}

QueueProducer.java

package com.javacodegeeks.example;

import java.util.concurrent.SynchronousQueue;

/**
 * 
 * @author anirudh
 *
 */
public class QueueProducer implements Runnable{

	private SynchronousQueue queue;
	
	public QueueProducer(SynchronousQueue queue) {
		this.queue=queue;
	}
	@Override
	public void run() {

		String event = "SYNCHRONOUS_EVENT";
		String another_event ="ANOTHER_EVENT";
		try {
			queue.put(event);
			System.out.printf("[%s] published event : %s %n", Thread
					.currentThread().getName(), event);
			
			queue.put(another_event);
			System.out.printf("[%s] published event : %s %n", Thread
					.currentThread().getName(), another_event);
			
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	
		
	}

}

QueueConsumer.java

package com.javacodegeeks.example;

import java.util.concurrent.SynchronousQueue;

/**
 * 
 * @author anirudh
 *
 */
public class QueueConsumer implements Runnable {

	private SynchronousQueue queue;

	public QueueConsumer(SynchronousQueue queue) {
		this.queue=queue;
	}

	@Override
	public void run() {
		try {
			String event = queue.take();
			// thread will block here
			System.out.printf("[%s] consumed event : %s %n", Thread
					.currentThread().getName(), event);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	
	}
	
}

Output :

[Thread-1] consumed event : SYNCHRONOUS_EVENT 
[Thread-0] published event : SYNCHRONOUS_EVENT 

In the example we saw that the event “SYNCHRONOUS_EVENT” was consumed by the consumer thread as soon as it was produced by the producer thread. And, for the other event “ANOTHER_EVENT” could not be published as there was no consumer thread available for it to be consumed. Hence, in a synchronous queue, without the consumer, producer will not be able to produce and vice versa.

Download the source code

So, in this example we saw how to use Java.util.concurrent.SynchronousQueue in java.

Download
You can download the full source code of this example here : JavaSynchronousQueueExample.zip
(No Ratings Yet)
Start the discussion Views Tweet it!

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

 

Leave a Reply

avatar
  Subscribe  
Notify of