Created
August 20, 2010 12:49
-
-
Save wolfc/540228 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
import java.util.concurrent.*; | |
import static java.util.concurrent.TimeUnit.SECONDS; | |
/** | |
* @author <a href="mailto:[email protected]">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