@Override public Runnable take() throws InterruptedException { // try to empty the task queue Runnable task; if (threadSafeQueue) { task = taskQueue.poll(); } else { synchronized (taskQueue) { task = taskQueue.poll(); } } if (task != null) { return task; } // block and wait for a task to be submitted return super.take(); }
@Override public OAuthTokenFinish call() throws Exception { logger.log(Level.INFO, "Waiting for token response"); final String urlWithIdAndToken = (String) ioQueue.take(); logger.log(Level.INFO, "Parsing token response " + urlWithIdAndToken); OAuthTokenFinish tokenResponse = null; // null if parsing failed (user canceled, api error, ... try { tokenResponse = extractor.parse(urlWithIdAndToken); ioQueue.put(OAuthWebResponses.createValidResponse()); } catch (NoSuchFieldException e) { logger.log(Level.SEVERE, "Unable to find token in response", e); ioQueue.put(OAuthWebResponses.createBadResponse()); } ioQueue.take(); // make sure undertow has send a response stop(); logger.log(Level.INFO, tokenResponse != null ? "Returning token" : "No token received, returning null"); return tokenResponse; }
@Override public String urlArg(String argName) { try { Comm c = getUrlArgComm(); HashMap<String, Serializable> data = new HashMap<>(); HashMap<String, Serializable> state = new HashMap<>(); state.put("name", "URL_ARG"); state.put("arg_name", argName); data.put("url", KernelManager.get().getBeakerXServer().getURL() + URL_ARG); data.put("type", "rest"); data.put("state", state); data.put("buffer_paths", new HashMap<>()); c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data)); // block Object argNameValue = getMessageQueue(URL_ARG).take(); return (String) argNameValue; } catch (Exception e) { throw new RuntimeException(e); } }
@Override public List<CodeCell> getCodeCells(String tagFilter) { // first send message to get cells try { Comm c = getCodeCellsComm(); HashMap<String, Serializable> data = new HashMap<>(); HashMap<String, Serializable> state = new HashMap<>(); state.put("name", "CodeCells"); state.put("value", getJson(tagFilter)); data.put("url", KernelManager.get().getBeakerXServer().getURL() + CODE_CELL_PATH); data.put("state", state); data.put("buffer_paths", new HashMap<>()); c.send(COMM_MSG, Comm.Buffer.EMPTY, new Comm.Data(data)); // block Object cells = getMessageQueue("CodeCells").take(); return (List<CodeCell>) cells; } catch (Exception e) { throw new RuntimeException(e); } }
/** * Process task. * * @param task the task to be processed * @throws InterruptedException when thread has been interrupted */ public void process(WebcamTask task) throws InterruptedException { inbound.put(task); Throwable t = outbound.take().getThrowable(); if (t != null) { throw new WebcamException("Cannot execute task", t); } }
@Override public void run() { while (true) { WebcamTask t = null; try { (t = inbound.take()).handle(); } catch (InterruptedException e) { break; } catch (Throwable e) { if (t != null) { t.setThrowable(e); } } finally { if (t != null) { try { outbound.put(t); } catch (InterruptedException e) { break; } catch (Exception e) { throw new RuntimeException("Cannot put task into outbound queue", e); } } } } } }
latestTask = this.task.take(); // blocking } finally { synchronized (this) {
@Override public Long call() throws Exception { ExcerptTailer tailer = queue.createTailer(); tailer.moveToIndex(startIndex); Long fromWriter = sync.take(); long index = doReadBad(tailer, messages + 50, false); if (index != fromWriter) { System.out.println("Reader:1 Not the same:" + index + " vs. " + fromWriter); } sync.put(index); fromWriter = sync.take(); index = doReadBad(tailer, 50, false); if (index != fromWriter) { System.out.println("Reader:2 Not the same:" + index + " vs. " + fromWriter); } return index; } }
while (stackTrace == null) { try { stackTrace = sync.take(); } catch (InterruptedException e) {
@Override public void handleRequest(HttpServerExchange exchange) throws Exception { final String urlWithIdAndToken = exchange.getRequestURL() + "?" + exchange.getQueryString(); logger.log(Level.INFO, "Got a request to " + urlWithIdAndToken); queue.add(urlWithIdAndToken); TimeUnit.SECONDS.sleep(2); final OAuthWebResponse oauthWebResponse = (OAuthWebResponse) queue.take(); logger.log(Level.INFO, "Got an oauth response with code " + oauthWebResponse.getCode()); exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "text/html"); exchange.setResponseCode(oauthWebResponse.getCode()); exchange.getResponseSender().send(oauthWebResponse.getBody()); exchange.endExchange(); queue.add(Boolean.TRUE); } }
@Override protected Versioned<Slop> computeNext() { try { Versioned<Slop> head = null; if(!shutDown) { head = slopQueue.take(); if(head.equals(END)) { shutDown = true; isComplete = true; } else { slopsDone++; if(slopsDone % voldemortConfig.getSlopBatchSize() == 0) { shutDown = true; } writeThrottler.maybeThrottle(writtenLast); writtenLast = slopSize(head); deleteBatch.add(Pair.create(head.getValue().makeKey(), (Version) head.getVersion())); return head; } } return endOfData(); } catch(Exception e) { logger.error("Got an exception " + e); return endOfData(); } }
@Override public void run() { assertSame(this, Thread.currentThread()); try { while (true) { Request request = requestQueue.take(); Object result; try { result = invokeMethod(request.methodName, request.arguments); } catch (ThreadDeath death) { return; } catch (InvocationTargetException exception) { responseQueue.put(new Response(request.methodName, null, exception.getTargetException())); continue; } catch (Throwable throwable) { responseQueue.put(new Response(request.methodName, null, throwable)); continue; } responseQueue.put(new Response(request.methodName, result, null)); } } catch (ThreadDeath death) { return; } catch (InterruptedException ignored) { // SynchronousQueue sometimes throws InterruptedException while the threads are stopping. } catch (Throwable uncaught) { this.uncaughtThrowable = uncaught; } }
synchronousQueue.take();
selected = sq.take(); } catch (InterruptedException ex) {
@Override public Long call() throws Exception { queue = ChronicleQueue.singleBuilder(path) //.testBlockSize() //.rollCycle(TEST_DAILY) .buffered(false) .build(); appender = queue.acquireAppender(); for (int i = 0; i < 50; i++) { appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world2 "))); } long index = appender.lastIndexAppended(); sync.put(index); Long fromReader = sync.take(); if (index != fromReader) { System.out.println("Writer:Not the same:" + index + " vs. " + fromReader); } for (int i = 0; i < 50; i++) { appender.writeDocument(wireOut -> wireOut.write("log").marshallable(m -> m.write("msg").text("hello world2 "))); } index = appender.lastIndexAppended(); sync.put(index); return index; } }
Object next = nextLog.take(); if (next instanceof Exception) { throw (Exception) next;
private IRubyObject peekTake() { try { return lastValue = out.take(); } catch (InterruptedException ie) { throw runtime.newThreadError("interrupted during iteration"); } }
private IRubyObject peekTake() { try { return lastValue = out.take(); } catch (InterruptedException ie) { throw runtime.newThreadError("interrupted during iteration"); } }
private IRubyObject take() { try { if (lastValue != null) { return lastValue; } return out.take(); } catch (InterruptedException ie) { throw runtime.newThreadError("interrupted during iteration"); } finally { lastValue = null; } }
private IRubyObject take() { try { if (lastValue != null) { return lastValue; } return out.take(); } catch (InterruptedException ie) { throw runtime.newThreadError("interrupted during iteration"); } finally { lastValue = null; } }