Skip to content

Instantly share code, notes, and snippets.

@wolfc
Created August 20, 2010 12:49
Show Gist options
  • Save wolfc/540228 to your computer and use it in GitHub Desktop.
Save wolfc/540228 to your computer and use it in GitHub Desktop.
import java.util.*;
import java.util.concurrent.*;
import static java.util.concurrent.TimeUnit.SECONDS;
/**
* @author <a href="mailto:cdewolf@redhat.com">Carlo de Wolf</a>
*/
public class LinkedBlockingQueueTestCase
{
public static void main(String args[])
throws ExecutionException, TimeoutException, InterruptedException, BrokenBarrierException
{
final LinkedBlockingQueue<Number> queue = new LinkedBlockingQueue<Number>();
List<FutureTask<Number>> futures = new ArrayList<FutureTask<Number>>();
Thread threads[] = new Thread[2];
Callable<Number> callable = new Callable<Number>()
{
@Override
public Number call() throws Exception
{
return queue.poll();
}
};
{
FutureTask<Number> target = new FutureTask<Number>(callable);
futures.add(target);
threads[0] = new Thread(target);
threads[0].start();
}
{
FutureTask<Number> target = new FutureTask<Number>(callable);
futures.add(target);
threads[1] = new Thread(target);
threads[1].start();
}
queue.offer(0);
queue.offer(1);
Number result = futures.get(0).get(5, SECONDS);
if(result == null)
throw new AssertionError("result can not be null");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment