| Refresh | Home EGTry.com

basic syntax of using SynchronousQueue -blocking take and put methods


take method blocking if no one is put anything into the queue

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread.queue;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueue1Test {

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

    SynchronousQueue<String> queue=new SynchronousQueue();
    System.out.println("taking a work item from queue");
    String item=queue.take();//block
    
    System.out.println("item take from queue: "+item);

  }
}



Output

taking a work item from queue


put method blocking if no one is taking item from the queue

/*
 * Copyright@ 2011 www.egtry.com
 */

package egtry.thread.queue;

import java.util.concurrent.SynchronousQueue;

public class SynchronousQueue2Test {

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

    SynchronousQueue<String> queue=new SynchronousQueue();
    System.out.println("putting a item into the queu");
    queue.put("first"); //block
    
    System.out.println("Done");

  }
}