Refine search
class MyHouse { final BlockingQueue<Object> queue = new LinkedBlockingQueue<>(); void eatFood() throws InterruptedException { // take next item from the queue (sleeps while waiting) Object food = queue.take(); // and do something with it System.out.println("Eating: " + food); } void deliverPizza() throws InterruptedException { // in producer threads, we push items on to the queue. // if there is space in the queue we can return immediately; // the consumer thread(s) will get to it later queue.put("A delicious pizza"); } }
/** * Wait for all threads to finish. * * @param destroyThreadpool -- if true, then destroy the worker threads * so that the main thread can shutdown. */ public void join(boolean destroyThreadpool) { // Make blocking calls to the last processes that are running if ( ! threadPool.isShutdown()) { try { for (int i = nThreads; i > 0; --i) { idleProcessors.take(); } if (destroyThreadpool) { threadPool.shutdown(); // Sanity check. The threadpool should be done after iterating over // the processors. threadPool.awaitTermination(10, TimeUnit.SECONDS); } else { // Repopulate the list of processors for (int i = 0; i < nThreads; ++i) { idleProcessors.put(i); } } } catch (InterruptedException e) { throw new RuntimeException(e); } } }
@Override public void run() { try { for (;;) { SystemFailure.checkFailure(); Runnable task = takeQueue.take(); if (forFnExec) { // In the function case, offer the request to the work queue. // If it fails, execute it anyway. This will cause the RejectedExecutionHandler to // spin off a thread for it. if (!putQueue.offer(task, OFFER_TIME, TimeUnit.MILLISECONDS)) { execute(task); } } else { // In the non-function case, put the request on the work queue. putQueue.put(task); } } } catch (InterruptedException ie) { Thread.currentThread().interrupt(); // this thread is being shutdown so just return; return; } } };
while (!Thread.currentThread().isInterrupted()) { try { UUID uuid = uuidQueue.take(); System.out.println("Consumed: " + uuid + " by " + Thread.currentThread().getName()); UUID randomUUID = UUID.randomUUID(); System.out.println("Produced: " + randomUUID + " by " + Thread.currentThread().getName()); uuidQueue.put(randomUUID);
/** * @param q Queue to test. * @throws InterruptedException If interrupted. */ private static long testBlockingQueue(BlockingQueue<Object> q) throws InterruptedException { GridTimer timer = new GridTimer("blocking-queue"); for (int i = 0; i < CNT; i++) q.put(new Object()); for (int i = 0; i < CNT; i++) { Object o = q.take(); assert o != null; } timer.stop(); return timer.duration(); } }
@Test public void shouldWaitForOngoingForceToCompleteBeforeForcingAgain() throws Throwable { channelCommandQueue.put( ChannelCommand.dummy ); // The 'emptyBuffer...' command will be put into the queue, and then it'll block on 'force' because the queue // will be at capacity. final BatchingTransactionAppender appender = life.add( createTransactionAppender() ); life.start(); Runnable runnable = createForceAfterAppendRunnable( appender ); Future<?> future = executor.submit( runnable ); forceSemaphore.acquire(); Thread otherThread = fork( runnable ); awaitThreadState( otherThread, MILLISECONDS_TO_WAIT, Thread.State.TIMED_WAITING ); assertThat( channelCommandQueue.take(), is( ChannelCommand.dummy ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) ); future.get(); otherThread.join(); assertTrue( channelCommandQueue.isEmpty() ); }
@Test public void shouldBatchUpMultipleWaitingForceRequests() throws Throwable channelCommandQueue.put( ChannelCommand.dummy ); assertThat( channelCommandQueue.take(), is( ChannelCommand.dummy ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.emptyBufferIntoChannelAndClearIt ) ); assertThat( channelCommandQueue.take(), is( ChannelCommand.force ) );
@Test public void testExplicitlySpecifiedPriorities() throws IOException, InterruptedException { final ExecutableFlow flow1 = createExecutableFlow("exec1", 5, 3, 1); final ExecutableFlow flow2 = createExecutableFlow("exec2", 6, 3, 2); final ExecutableFlow flow3 = createExecutableFlow("exec3", 2, 3, 3); final ExecutionReference dummyRef = new ExecutionReference(0); final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue = new PriorityBlockingQueue<>(10, new ExecutableFlowPriorityComparator()); queue.put(new Pair<>(dummyRef, flow1)); queue.put(new Pair<>(dummyRef, flow2)); queue.put(new Pair<>(dummyRef, flow3)); Assert.assertEquals(flow2, queue.take().getSecond()); Assert.assertEquals(flow1, queue.take().getSecond()); Assert.assertEquals(flow3, queue.take().getSecond()); }
@Test public void testMixedSpecifiedPriorities() throws IOException, InterruptedException { final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 3, 1); final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 3, 2); final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 3, 3); final ExecutionReference dummyRef = new ExecutionReference(0); final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue = new PriorityBlockingQueue<>(10, new ExecutableFlowPriorityComparator()); queue.put(new Pair<>(dummyRef, flow1)); queue.put(new Pair<>(dummyRef, flow2)); queue.put(new Pair<>(dummyRef, flow3)); Assert.assertEquals(flow3, queue.take().getSecond()); Assert.assertEquals(flow1, queue.take().getSecond()); Assert.assertEquals(flow2, queue.take().getSecond()); }
@Test public void testEqualPriorities() throws IOException, InterruptedException { final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 1, 1); final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 2, 2); final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 3, 3); final ExecutableFlow flow4 = createExecutableFlow("exec3", 3, 4, 4); final ExecutionReference dummyRef = new ExecutionReference(0); final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue = new PriorityBlockingQueue<>(10, new ExecutableFlowPriorityComparator()); queue.put(new Pair<>(dummyRef, flow4)); queue.put(new Pair<>(dummyRef, flow1)); queue.put(new Pair<>(dummyRef, flow2)); queue.put(new Pair<>(dummyRef, flow3)); Assert.assertEquals(flow3, queue.take().getSecond()); Assert.assertEquals(flow1, queue.take().getSecond()); Assert.assertEquals(flow4, queue.take().getSecond()); Assert.assertEquals(flow2, queue.take().getSecond()); }
@Test public void testEqualUpdateTimeAndPriority() throws IOException, InterruptedException { final ExecutableFlow flow1 = createExecutableFlow("exec1", 3, 1, 1); final ExecutableFlow flow2 = createExecutableFlow("exec2", 2, 2, 2); final ExecutableFlow flow3 = createExecutableFlow("exec3", -2, 2, 3); final ExecutableFlow flow4 = createExecutableFlow("exec3", 3, 4, 4); final ExecutionReference dummyRef = new ExecutionReference(0); final BlockingQueue<Pair<ExecutionReference, ExecutableFlow>> queue = new PriorityBlockingQueue<>(10, new ExecutableFlowPriorityComparator()); queue.put(new Pair<>(dummyRef, flow4)); queue.put(new Pair<>(dummyRef, flow1)); queue.put(new Pair<>(dummyRef, flow2)); queue.put(new Pair<>(dummyRef, flow3)); Assert.assertEquals(flow3, queue.take().getSecond()); Assert.assertEquals(flow1, queue.take().getSecond()); Assert.assertEquals(flow4, queue.take().getSecond()); Assert.assertEquals(flow2, queue.take().getSecond()); } }