Home  >  Core Java

Example of SynchronousQueue in Java

By Arvind Rai, December 18, 2012
1. SynchronousQueue implements BlockingQueue interface.
2. The put method waits fro take and take method waits for put.
3. No element can be added without the call of take and no element can be removed without the call of put.
4. It has no size, at synchronous point element is put and take.
5. SynchronousQueue allows optional fairness ordering for the threads that are waiting to take.
6. It does not allow null values.
package com.concretepage;
import java.util.concurrent.SynchronousQueue;
public class SynchronousQueueTest {
	private SynchronousQueue<String> sq = new SynchronousQueue<String>(true);  
	 class PutThread implements Runnable {
	     public void run() {
	        	 try {
					sq.put("A");
				} catch (InterruptedException e) {
						e.printStackTrace();
				}
	         }
	    }
	   class TakeThread implements Runnable {
	     public void run() {
	          	 try {
					System.out.println(sq.take());
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
	         }
	   }
	   public static void main(String... args) {
	     new Thread((new SynchronousQueueTest()).new PutThread()).start();
	     new Thread((new SynchronousQueueTest()).new TakeThread()).start();
	   }
}
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us