下面列出了java.util.concurrent.atomic.AtomicBoolean#wait() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
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);
}
}
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
{
synchronized (condition) {
while (!condition.get()) {
condition.wait();
}
}
}
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
{
synchronized (condition) {
while (!condition.get()) {
condition.wait();
}
}
}
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();
}
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");
}
}
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
{
synchronized (condition) {
while (!condition.get()) {
condition.wait();
}
}
}
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();
}
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();
}
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();
}
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();
}
}
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
{
synchronized (condition) {
while (!condition.get()) {
condition.wait();
}
}
}
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");
}
}
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();
}
public static void waitForConditionEx(final AtomicBoolean condition)
throws InterruptedException
{
synchronized (condition) {
while (!condition.get()) {
condition.wait();
}
}
}
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();
}
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");
}
}
@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());
}
}
@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();
}
}
public static void main(String[] args) throws IOException, InterruptedException {
// System.out.println("\u001B[1mfoo\u001B[[email protected]\u001B[[email protected]\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();
}
}
/**
* 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);
}