List of usage examples for java.util.concurrent.atomic AtomicBoolean notify
@HotSpotIntrinsicCandidate public final native void notify();
From source file:org.apache.blur.shell.QueryCommand.java
private void doItInternal(Blur.Iface client, String[] args, PrintWriter out) throws FinishedException, BlurException, TException { CommandLine commandLine = QueryCommandHelper.parse(args, out, name() + " " + usage()); if (commandLine == null) { return;// w w w . j ava 2 s. c om } BlurQuery blurQuery = QueryCommandHelper.getBlurQuery(commandLine); if (Main.debug) { out.println(blurQuery); } _width = 100; if (commandLine.hasOption(QueryCommandHelper.WIDTH)) { _width = Integer.parseInt(commandLine.getOptionValue(QueryCommandHelper.WIDTH)); } String tablename = args[1]; long s = System.nanoTime(); BlurResults blurResults = client.query(tablename, blurQuery); long e = System.nanoTime(); long timeInNanos = e - s; if (blurResults.getTotalResults() == 0) { out.println("No results found in [" + timeInNanos / 1000000.0 + " ms]."); return; } ConsoleReader reader = getConsoleReader(); if (reader == null) { String description = blurResults.getTotalResults() + " results found in [" + timeInNanos / 1000000.0 + " ms]. " + getFetchMetaData(blurResults); out.println(description); List<BlurResult> results = blurResults.getResults(); for (BlurResult result : results) { print(out, result); } return; } String prompt = reader.getPrompt(); reader.setPrompt(""); final TableDisplay tableDisplay = new TableDisplay(reader); tableDisplay.setDescription(white(blurResults.getTotalResults() + " results found in [" + timeInNanos / 1000000.0 + " ms]. " + getFetchMetaData(blurResults))); tableDisplay.setSeperator(" "); try { final AtomicBoolean viewing = new AtomicBoolean(true); tableDisplay.addKeyHook(new Runnable() { @Override public void run() { synchronized (viewing) { viewing.set(false); viewing.notify(); tableDisplay.setStopReadingInput(true); } } }, 'q'); RenderType type = getRenderRype(blurResults); switch (type) { case ROW_MULTI_FAMILY: renderRowMultiFamily(tableDisplay, blurResults); break; case ROW_SINGLE_FAMILY: renderRowSingleFamily(tableDisplay, blurResults); break; default: break; } while (viewing.get()) { synchronized (viewing) { try { viewing.wait(); } catch (InterruptedException ex) { throw new RuntimeException(ex); } } } } finally { try { tableDisplay.close(); } catch (IOException ex) { if (Main.debug) { ex.printStackTrace(); } } try { Thread.sleep(100); } catch (InterruptedException ex) { if (Main.debug) { ex.printStackTrace(); } } try { reader.setPrompt(""); reader.clearScreen(); } catch (IOException ex) { if (Main.debug) { ex.printStackTrace(); } } out.write("\u001B[0m"); out.flush(); reader.setPrompt(prompt); } }
From source file:org.apache.zeppelin.python.IPythonClient.java
public ExecuteResponse stream_execute(ExecuteRequest request, final InterpreterOutputStream interpreterOutput) { final ExecuteResponse.Builder finalResponseBuilder = ExecuteResponse.newBuilder() .setStatus(ExecuteStatus.SUCCESS); final AtomicBoolean completedFlag = new AtomicBoolean(false); LOGGER.debug("stream_execute code:\n" + request.getCode()); asyncStub.execute(request, new StreamObserver<ExecuteResponse>() { int index = 0; boolean isPreviousOutputImage = false; @Override/* w w w. j a v a 2 s . co m*/ public void onNext(ExecuteResponse executeResponse) { if (executeResponse.getType() == OutputType.TEXT) { try { LOGGER.debug("Interpreter Streaming Output: " + executeResponse.getOutput()); if (isPreviousOutputImage) { // add '\n' when switch from image to text interpreterOutput.write("\n%text ".getBytes()); } isPreviousOutputImage = false; interpreterOutput.write(executeResponse.getOutput().getBytes()); interpreterOutput.getInterpreterOutput().flush(); } catch (IOException e) { LOGGER.error("Unexpected IOException", e); } } if (executeResponse.getType() == OutputType.IMAGE) { try { LOGGER.debug("Interpreter Streaming Output: IMAGE_DATA"); if (index != 0) { // add '\n' if this is the not the first element. otherwise it would mix the image // with the text interpreterOutput.write("\n".getBytes()); } interpreterOutput.write(("%img " + executeResponse.getOutput()).getBytes()); interpreterOutput.getInterpreterOutput().flush(); isPreviousOutputImage = true; } catch (IOException e) { LOGGER.error("Unexpected IOException", e); } } if (executeResponse.getStatus() == ExecuteStatus.ERROR) { // set the finalResponse to ERROR if any ERROR happens, otherwise the finalResponse would // be SUCCESS. finalResponseBuilder.setStatus(ExecuteStatus.ERROR); } index++; } @Override public void onError(Throwable throwable) { try { interpreterOutput.getInterpreterOutput().write(ExceptionUtils.getStackTrace(throwable)); interpreterOutput.getInterpreterOutput().flush(); } catch (IOException e) { LOGGER.error("Unexpected IOException", e); } LOGGER.error("Fail to call IPython grpc", throwable); finalResponseBuilder.setStatus(ExecuteStatus.ERROR); completedFlag.set(true); synchronized (completedFlag) { completedFlag.notify(); } } @Override public void onCompleted() { synchronized (completedFlag) { try { LOGGER.debug("stream_execute is completed"); interpreterOutput.getInterpreterOutput().flush(); } catch (IOException e) { LOGGER.error("Unexpected IOException", e); } completedFlag.set(true); completedFlag.notify(); } } }); synchronized (completedFlag) { if (!completedFlag.get()) { try { completedFlag.wait(); } catch (InterruptedException e) { LOGGER.error("Unexpected Interruption", e); } } } return finalResponseBuilder.build(); }
From source file:org.polymap.core.runtime.UIJob.java
/** * Waits until this job is finished. This method will block the calling thread * until the job has finished executing, or until this thread has been * interrupted, or the timeout exceeds. A job must not be joined from within the * scope of its run method./*from w w w .java 2 s. c o m*/ * <p/> * This method must be called from the UIThread. It calls readAndDispatch() of * the current display, so that the UI is responsive during the call is blocked. * * @param timeoutMillis * @return False if the job did not finish within the given timeout or the * calling thread was interrupted. */ public boolean joinAndDispatch(long timeoutMillis) { final AtomicBoolean done = new AtomicBoolean(getState() == Job.NONE); IJobChangeListener jobListener = new JobChangeAdapter() { public void done(IJobChangeEvent event) { synchronized (done) { done.set(true); done.notify(); } } }; addJobChangeListener(jobListener); // check if job is done already - after the listener has been // registered; this avoids race cond. between the two if (getState() == Job.NONE) { removeJobChangeListener(jobListener); return true; } final Display threadDisplay = Display.getCurrent(); final Timer timer = new Timer(); while (!done.get() && timer.elapsedTime() < timeoutMillis && (threadDisplay == null || !threadDisplay.isDisposed())) { Thread.yield(); if (threadDisplay != null) { if (!threadDisplay.readAndDispatch()) { synchronized (done) { try { done.wait(300); log.debug("wake after: " + timer.elapsedTime() + "ms"); } catch (InterruptedException e) { } } // // just wait on #done blocks hangs; // // display.sleep() might wait forever, so we need this watchdog // Polymap.executorService().execute( new Runnable() { // public void run() { // synchronized (done) { // try { done.wait( 300 ); } catch (InterruptedException e) {} // } // log.info( "wake ..." ); // threadDisplay.wake(); // } // }); // threadDisplay.sleep(); } } else { synchronized (done) { try { done.wait(250); } catch (InterruptedException e) { } } } } removeJobChangeListener(jobListener); return done.get(); }