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); } } }
while (!Thread.currentThread().isInterrupted()) { try { Object message = mMessages.take(); if (message == MESSAGE_DONE) { break; mStdout.println(message); } else if (message instanceof CopyException) { CopyException e = (CopyException) message; mStderr.println(messageAndCause(e)); } else { LOG.error("Unsupported message type " + message.getClass()
System.out.print(doTest(new ArrayBlockingQueue<Integer>(length), N) + "\t"); System.out.print(doTest(new SynchronousQueue<Integer>(), N)); System.out.println(); public void run() { for (int i = 0; i < n; i++) try { q.put(i); } catch (InterruptedException ex) {} long sum = 0; for (int i = 0; i < n; i++) try { sum += q.take(); } catch (InterruptedException ex) {} return sum;
private boolean fetchChunk(final boolean block) throws InterruptedException { if (eof) { return false; } // Read until no buffers available (poll returned null) // or until a non-empty buffer or EOF is reached. do { if (closed.get()) { current = EOF; break; } current = (block) ? buffers.take() : buffers.poll(); } while (current != null && current != EOF && !current.hasRemaining()); eof = current == EOF; return !eof; }
public static final BlockingQueue<Runnable> queue = new LinkedBlockingQueue<Runnable>(); public static void main(String[] args) throws Exception { new Thread(new Runnable(){ @Override public void run() { final int result; result = 2+3; queue.add(new Runnable(){ @Override public void run() { System.out.println(result); } }); } }).start(); while(true) { queue.take().run(); } }
System.out.println("=== BlockingQueue ==="); System.out.println("Queue will execute for 10s"); 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);
private boolean fetchChunk(final boolean block) throws InterruptedException { if (eof) { return false; } // Read until no buffers available (poll returned null) // or until a non-empty buffer or EOF is reached. do { if (closed.get()) { current = EOF; break; } current = (block) ? buffers.take() : buffers.poll(); } while (current != null && current != EOF && !current.hasRemaining()); eof = current == EOF; return !eof; }
public void run() { try { bq.put("A"); bq.put("B"); bq.put("C"); bq.put("D"); bq.put("E"); public void run() { try { System.out.println("1 = " + bq.take()); System.out.println("2 = " + bq.take()); System.out.println("3 = " + bq.take()); System.out.println("4 = " + bq.take()); System.out.println("5 = " + bq.take()); System.out.println("6 = " + bq.take()); } catch (InterruptedException ex) { Thread.currentThread().interrupt();
@Override public void run() { WebSocketImpl ws = null; try { while ( true ) { ByteBuffer buf; ws = iqueue.take(); buf = ws.inQueue.poll(); assert ( buf != null ); doDecode(ws, buf); ws = null; } } catch ( InterruptedException e ) { Thread.currentThread().interrupt(); } catch ( RuntimeException e ) { handleFatal( ws, 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; } } };
public void acceptInput() throws InterruptedException { boolean wasAttached = debugger.isAttached(); if (!wasAttached) { System.out.println("Waiting for remote process to attach..."); } while (true) { queue.take().run(); if (debugger.isAttached() && !wasAttached) { wasAttached = true; System.out.println("Attached"); new Thread(() -> { try { stdinThread(); } catch (IOException e) { e.printStackTrace(); } }).start(); } else if (!debugger.isAttached() && wasAttached) { break; } } queue.offer(() -> { debugger.detach(); server.stop(); }); }
private final BlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>(); ... @Override public onDataAvailable(int val){ System.out.println("listener received val: " + val); queue.put(val); } ... generatorThread.addListener(listener); generatorThread.start(); while (true) { // this waits for the queue to get a value int val = queue.take(); System.out.println("value is: " + val); }
private RandomAccessFile checkOut() throws IOException, InterruptedException { RandomAccessFile fileHandle = readFileHandles.poll(); if (fileHandle != null) { return fileHandle; } int remaining = readFileHandles.remainingCapacity(); if (remaining > 0) { LOG.info("Opening " + file + " for read, remaining number of file " + "handles available for reads of this file is " + remaining); return open(); } return readFileHandles.take(); }