Java Code Examples for java.util.concurrent.atomic.AtomicBoolean#wait()

The following examples show how to use java.util.concurrent.atomic.AtomicBoolean#wait() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TaskManager.java    From FastAsyncWorldedit with GNU General Public License v3.0 6 votes vote down vote up
public void wait(AtomicBoolean running, int timout) {
    try {
        long start = System.currentTimeMillis();
        synchronized (running) {
            while (running.get()) {
                running.wait(timout);
                if (running.get() && System.currentTimeMillis() - start > Settings.IMP.QUEUE.DISCARD_AFTER_MS) {
                    new RuntimeException("FAWE is taking a long time to execute a task (might just be a symptom): ").printStackTrace();
                    Fawe.debug("For full debug information use: /fawe threads");
                }
            }
        }
    } catch (InterruptedException e) {
        MainUtil.handleError(e);
    }
}
 
Example 2
Source File: Util.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 3
Source File: TestAlwaysOnTopBeforeShow.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
static void waitFocused(Window w, AtomicBoolean b) {
    try {
        synchronized(b) {
            if (w.isFocusOwner()) {
                return;
            }
            b.wait(3000);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!w.isFocusOwner()) {
        throw new RuntimeException("Can't make " + w + " focus owner");
    }
}
 
Example 4
Source File: Util.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 5
Source File: Util.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
  {
      synchronized (condition) {
          while (!condition.get()) {
              condition.wait();
          }
      }
  }
 
Example 6
Source File: Util.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 7
Source File: TestAlwaysOnTopBeforeShow.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
static void waitFocused(Window w, AtomicBoolean b) {
    try {
        synchronized(b) {
            if (w.isFocusOwner()) {
                return;
            }
            b.wait(3000);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!w.isFocusOwner()) {
        throw new RuntimeException("Can't make " + w + " focus owner");
    }
}
 
Example 8
Source File: Util.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
  {
      synchronized (condition) {
          while (!condition.get()) {
              condition.wait();
          }
      }
  }
 
Example 9
Source File: TestPreemption.java    From tez with Apache License 2.0 5 votes vote down vote up
void syncWithMockAppLauncher(boolean allowScheduling, AtomicBoolean mockAppLauncherGoFlag, 
    MockTezClient tezClient) throws Exception {
  synchronized (mockAppLauncherGoFlag) {
    while (!mockAppLauncherGoFlag.get()) {
      mockAppLauncherGoFlag.wait();
    }
    mockApp = tezClient.getLocalClient().getMockApp();
    mockLauncher = mockApp.getContainerLauncher();
    mockLauncher.startScheduling(allowScheduling);
    mockAppLauncherGoFlag.notify();
  }     
}
 
Example 10
Source File: Util.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
  {
      synchronized (condition) {
          while (!condition.get()) {
              condition.wait();
          }
      }
  }
 
Example 11
Source File: Util.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 12
Source File: Util.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 13
Source File: Util.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
  {
      synchronized (condition) {
          while (!condition.get()) {
              condition.wait();
          }
      }
  }
 
Example 14
Source File: TestAlwaysOnTopBeforeShow.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
static void waitFocused(Window w, AtomicBoolean b) {
    try {
        synchronized(b) {
            if (w.isFocusOwner()) {
                return;
            }
            b.wait(3000);
        }
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    if (!w.isFocusOwner()) {
        throw new RuntimeException("Can't make " + w + " focus owner");
    }
}
 
Example 15
Source File: Util.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public static boolean waitForConditionEx(final AtomicBoolean condition, long timeout)
throws InterruptedException
  {
      synchronized (condition) {
          long startTime = System.currentTimeMillis();
          while (!condition.get()) {
              condition.wait(timeout);
              if (System.currentTimeMillis() - startTime >= timeout ) {
                  break;
              }
          }
      }
      return condition.get();
  }
 
Example 16
Source File: Util.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
  {
      synchronized (condition) {
          while (!condition.get()) {
              condition.wait();
          }
      }
  }
 
Example 17
Source File: BaseStepTest.java    From pentaho-kettle with Apache License 2.0 4 votes vote down vote up
@Test
public void testStepListenersConcurrentModification() throws InterruptedException {
  // Create a base step
  final BaseStep baseStep =
    new BaseStep( mockHelper.stepMeta, mockHelper.stepDataInterface, 0, mockHelper.transMeta, mockHelper.trans );

  // Create thread to dynamically add listeners
  final AtomicBoolean done = new AtomicBoolean( false );
  Thread addListeners = new Thread() {
    @Override
    public void run() {
      while ( !done.get() ) {
        baseStep.addStepListener( mock( StepListener.class ) );
        synchronized ( done ) {
          done.notify();
        }
      }
    }
  };

  // Mark start and stop while listeners are being added
  try {
    addListeners.start();

    // Allow a few listeners to be added
    synchronized ( done ) {
      while ( baseStep.getStepListeners().size() < 20 ) {
        done.wait();
      }
    }

    baseStep.markStart();

    // Allow more listeners to be added
    synchronized ( done ) {
      while ( baseStep.getStepListeners().size() < 100 ) {
        done.wait();
      }
    }

    baseStep.markStop();

  } finally {
    // Close addListeners thread
    done.set( true );
    addListeners.join();
  }
}
 
Example 18
Source File: IOManagerAsyncTest.java    From flink with Apache License 2.0 4 votes vote down vote up
@Test
public void testExceptionPropagationReader() {
	try {
		// use atomic boolean as a boolean reference
		final AtomicBoolean handlerCalled = new AtomicBoolean();
		final AtomicBoolean exceptionForwarded = new AtomicBoolean();
		
		ReadRequest req = new ReadRequest() {
			
			@Override
			public void requestDone(IOException ioex) {
				if (ioex instanceof TestIOException) {
					exceptionForwarded.set(true);
				}
				
				synchronized (handlerCalled) {
					handlerCalled.set(true);
					handlerCalled.notifyAll();
				}
			}
			
			@Override
			public void read() throws IOException {
				throw new TestIOException();
			}
		};
		
		
		// test the read queue
		RequestQueue<ReadRequest> rq = ioManager.getReadRequestQueue(ioManager.createChannel());
		rq.add(req);

		// wait until the asynchronous request has been handled
		synchronized (handlerCalled) {
			while (!handlerCalled.get()) {
				handlerCalled.wait();
			}
		}
		
		assertTrue(exceptionForwarded.get());
	}
	catch (Exception e) {
		e.printStackTrace();
		fail(e.getMessage());
	}
}
 
Example 19
Source File: TableDisplay.java    From incubator-retired-blur with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws IOException, InterruptedException {

    // System.out.println("\u001B[1mfoo\u001B[0m@bar\u001B[32m@baz\u001B[0m>");

    // \u001B[0m normal
    // \u001B[33m yellow

    // System.out.println("\u001B[33mCOLUMN\u001B[0mnormal");
    //
    // for (int i = 0; i < 100; i++) {
    // System.out.println("\u001B[0m" + i + "\u001B[" + i + "mfoo");
    // }

    ConsoleReader reader = new ConsoleReader();
    TableDisplay tableDisplay = new TableDisplay(reader);
    tableDisplay.setSeperator("|");
    // Random random = new Random();
    int maxX = 20;
    int maxY = 100;
    tableDisplay.setHeader(0, "");
    for (int i = 1; i < maxX; i++) {
      tableDisplay.setHeader(i, "col-" + i);
    }

    for (int i = 0; i < maxY; i++) {
      tableDisplay.set(0, i, i);
    }

    final AtomicBoolean running = new AtomicBoolean(true);

    tableDisplay.addKeyHook(new Runnable() {
      @Override
      public void run() {
        synchronized (running) {
          running.set(false);
          running.notifyAll();
        }
      }
    }, 'q');

    try {
      // int i = 0;
      // while (true) {
      // if (i >= 100000) {
      // i = 0;
      // }
      // tableDisplay.set(random.nextInt(maxX) + 1, random.nextInt(maxY),
      // random.nextLong());
      // Thread.sleep(3000);
      // i++;
      // }
      for (int x = 0; x < maxX; x++) {
        for (int y = 0; y < maxY; y++) {
          if (x == 7 && y == 7) {
            tableDisplay.set(x, y, highlight(x + "," + y));
          } else {
            tableDisplay.set(x, y, x + "," + y);
          }
        }
      }
      while (running.get()) {
        synchronized (running) {
          running.wait(1000);
        }
      }
    } finally {
      tableDisplay.close();
    }
  }
 
Example 20
Source File: OpenRegionHandler.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Update ZK or META.  This can take a while if for example the
 * hbase:meta is not available -- if server hosting hbase:meta crashed and we are
 * waiting on it to come back -- so run in a thread and keep updating znode
 * state meantime so master doesn't timeout our region-in-transition.
 * Caller must cleanup region if this fails.
 */
private boolean updateMeta(final HRegion r, long masterSystemTime) {
  if (this.server.isStopped() || this.rsServices.isStopping()) {
    return false;
  }
  // Object we do wait/notify on.  Make it boolean.  If set, we're done.
  // Else, wait.
  final AtomicBoolean signaller = new AtomicBoolean(false);
  PostOpenDeployTasksThread t = new PostOpenDeployTasksThread(r,
    this.server, this.rsServices, signaller, masterSystemTime);
  t.start();
  // Post open deploy task:
  //   meta => update meta location in ZK
  //   other region => update meta
  while (!signaller.get() && t.isAlive() && !this.server.isStopped() &&
      !this.rsServices.isStopping() && isRegionStillOpening()) {
    synchronized (signaller) {
      try {
        // Wait for 10 seconds, so that server shutdown
        // won't take too long if this thread happens to run.
        if (!signaller.get()) signaller.wait(10000);
      } catch (InterruptedException e) {
        // Go to the loop check.
      }
    }
  }
  // Is thread still alive?  We may have left above loop because server is
  // stopping or we timed out the edit.  Is so, interrupt it.
  if (t.isAlive()) {
    if (!signaller.get()) {
      // Thread still running; interrupt
      LOG.debug("Interrupting thread " + t);
      t.interrupt();
    }
    try {
      t.join();
    } catch (InterruptedException ie) {
      LOG.warn("Interrupted joining " +
        r.getRegionInfo().getRegionNameAsString(), ie);
      Thread.currentThread().interrupt();
    }
  }

  // Was there an exception opening the region?  This should trigger on
  // InterruptedException too.  If so, we failed.
  return (!Thread.interrupted() && t.getException() == null);
}