Java Code Examples for java.util.concurrent.TimeUnit#timedWait()

The following examples show how to use java.util.concurrent.TimeUnit#timedWait() . 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: AbstractResultCollector.java    From spring-boot-data-geode with Apache License 2.0 6 votes vote down vote up
@Override
public S getResult(long duration, TimeUnit unit) throws FunctionException, InterruptedException {

	unit = resolveTimeUnit(unit);

	long durationInMilliseconds = unit.toMillis(duration);
	long timeout = System.currentTimeMillis() + unit.toMillis(duration);
	long waitInMilliseconds = Math.max(50, Math.min(durationInMilliseconds / 5, durationInMilliseconds));

	synchronized (this) {
		while (getResult() == null && System.currentTimeMillis() < timeout) {
			unit.timedWait(this, waitInMilliseconds);
		}
	}

	return getResult();
}
 
Example 2
Source File: LocatorLauncher.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Waits for a Locator status request response to be returned up to the specified timeout in the given unit of time.
 * This call will send status requests at fixed intervals in the given unit of time until the timeout expires.  If
 * the request to determine the Locator's status is successful, then the Locator is considered to be 'ONLINE'.
 * Otherwise, the Locator is considered to be unresponsive to the status request.
 * 
 * However, this does not necessarily imply the Locator start was unsuccessful, only that a response was not received
 * in the given time period.
 * 
 * Note, this method does not block or cause the Locator's location-based services (daemon Threads) to continue
 * running in anyway if the main application Thread terminates when running the Locator in-process.  If the caller
 * wishes to start a Locator in an asynchronous manner within the application process, then a call should be made to
 * <code>waitOnLocator</code>.
 * 
 * @param timeout a long value in time unit indicating when the period of time should expire in attempting
 * to determine the Locator's status.
 * @param interval a long value in time unit for how frequent the requests should be sent to the Locator.
 * @param timeUnit the unit of time in which the timeout and interval are measured.
 * @return the state of the Locator, which will either be 'ONLINE' or "NOT RESPONDING'.  If the status returned is
 * 'NOT RESPONDING', it just means the Locator did not respond to the status request within the given time period.
 * It should not be taken as the Locator failed to start.
 * @see #waitOnLocator()
 */
public LocatorState waitOnStatusResponse(final long timeout, final long interval, final TimeUnit timeUnit) {
  final long endTimeInMilliseconds = (System.currentTimeMillis() + timeUnit.toMillis(timeout));

  while (System.currentTimeMillis() < endTimeInMilliseconds) {
    try {
      final LocatorStatusResponse response = InternalLocator.statusLocator(getPort(), getBindAddress());
      return new LocatorState(this, Status.ONLINE, response);
    }
    catch (Exception ignore) {
      try {
        synchronized (this) {
          timeUnit.timedWait(this, interval);
        }
      }
      catch (InterruptedException ignoreInterrupt) {
        // NOTE just go and send another status request to the Locator...
      }
    }
  }

  // NOTE just because we were not able to communicate with the Locator in the given amount of time does not mean
  // the Locator is having problems.  The Locator could be slow in starting up and the timeout may not be
  // long enough.
  return new LocatorState(this, Status.NOT_RESPONDING);
}
 
Example 3
Source File: LockImpl.java    From HolandaCatalinaFw with Apache License 2.0 6 votes vote down vote up
@Override
public boolean await(long time, TimeUnit unit) throws InterruptedException {
    boolean result;
    if(Thread.currentThread().getId() == lock.lockedId) {
        CloudOrchestrator.getInstance().unlock(Lock.class.getName(), lock.name, LOCK_NAME);
        long startTime = System.currentTimeMillis();
        synchronized (this) {
            unit.timedWait(this, time);
        }
        result = (System.currentTimeMillis() - startTime) >= unit.toMillis(time);
        CloudOrchestrator.getInstance().lock(Lock.class.getName(), lock.name, LOCK_NAME);
    } else {
        throw new IllegalMonitorStateException();
    }
    return result;
}
 
Example 4
Source File: LocatorLauncher.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Waits for a Locator status request response to be returned up to the specified timeout in the given unit of time.
 * This call will send status requests at fixed intervals in the given unit of time until the timeout expires.  If
 * the request to determine the Locator's status is successful, then the Locator is considered to be 'ONLINE'.
 * Otherwise, the Locator is considered to be unresponsive to the status request.
 * 
 * However, this does not necessarily imply the Locator start was unsuccessful, only that a response was not received
 * in the given time period.
 * 
 * Note, this method does not block or cause the Locator's location-based services (daemon Threads) to continue
 * running in anyway if the main application Thread terminates when running the Locator in-process.  If the caller
 * wishes to start a Locator in an asynchronous manner within the application process, then a call should be made to
 * <code>waitOnLocator</code>.
 * 
 * @param timeout a long value in time unit indicating when the period of time should expire in attempting
 * to determine the Locator's status.
 * @param interval a long value in time unit for how frequent the requests should be sent to the Locator.
 * @param timeUnit the unit of time in which the timeout and interval are measured.
 * @return the state of the Locator, which will either be 'ONLINE' or "NOT RESPONDING'.  If the status returned is
 * 'NOT RESPONDING', it just means the Locator did not respond to the status request within the given time period.
 * It should not be taken as the Locator failed to start.
 * @see #waitOnLocator()
 */
public LocatorState waitOnStatusResponse(final long timeout, final long interval, final TimeUnit timeUnit) {
  final long endTimeInMilliseconds = (System.currentTimeMillis() + timeUnit.toMillis(timeout));

  while (System.currentTimeMillis() < endTimeInMilliseconds) {
    try {
      final LocatorStatusResponse response = InternalLocator.statusLocator(getPort(), getBindAddress());
      return new LocatorState(this, Status.ONLINE, response);
    }
    catch (Exception ignore) {
      try {
        synchronized (this) {
          timeUnit.timedWait(this, interval);
        }
      }
      catch (InterruptedException ignoreInterrupt) {
        // NOTE just go and send another status request to the Locator...
      }
    }
  }

  // NOTE just because we were not able to communicate with the Locator in the given amount of time does not mean
  // the Locator is having problems.  The Locator could be slow in starting up and the timeout may not be
  // long enough.
  return new LocatorState(this, Status.NOT_RESPONDING);
}
 
Example 5
Source File: SystemProcessingTimeService.java    From Flink-CEPplus with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(long timeout, @Nonnull TimeUnit unit) throws InterruptedException, TimeoutException {
	synchronized (lock) {
		while (!canceled) {
			unit.timedWait(lock, timeout);
		}

		if (canceled) {
			throw new CancellationException();
		} else {
			throw new TimeoutException();
		}
	}
}
 
Example 6
Source File: NeverCompleteFuture.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(long timeout, @Nonnull TimeUnit unit) throws InterruptedException, TimeoutException {
	synchronized (lock) {
		while (!canceled) {
			unit.timedWait(lock, timeout);
		}

		if (canceled) {
			throw new CancellationException();
		} else {
			throw new TimeoutException();
		}
	}
}
 
Example 7
Source File: ZigBeeTransactionFuture.java    From com.zsmartsystems.zigbee with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public CommandResult get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException {
    synchronized (this) {
        if (result != null) {
            return result;
        }
        unit.timedWait(this, timeout);
        if (result == null) {
            set(new CommandResult(ZigBeeStatus.FAILURE, null));
        }
        return result;
    }
}
 
Example 8
Source File: NeverCompleteFuture.java    From flink with Apache License 2.0 5 votes vote down vote up
@Override
public Object get(long timeout, @Nonnull TimeUnit unit) throws InterruptedException, TimeoutException {
	synchronized (lock) {
		while (!canceled) {
			unit.timedWait(lock, timeout);
		}

		if (canceled) {
			throw new CancellationException();
		} else {
			throw new TimeoutException();
		}
	}
}
 
Example 9
Source File: CommandResultFuture.java    From zigbee4java with Apache License 2.0 5 votes vote down vote up
@Override
public CommandResult get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
    synchronized (this) {
        if (result != null) {
            return result;
        }
        unit.timedWait(this, timeout);
        if (result == null) {
            set(new CommandResult());
            zigBeeApi.removeCommandExecution(commandExecution);
        }
        return result;
    }
}
 
Example 10
Source File: Promise.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public java.util.concurrent.Future<V> getFuture() {
    return new Future<V>() {
        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }

        @Override
        public boolean isDone() {
            synchronized (mutex) {
                return done;
            }
        }

        @Override
        public V get() throws InterruptedException, ExecutionException {
            synchronized (mutex) {
                while (!done) mutex.wait();
                if (error != null) throw error;
                return value;
            }
        }

        @Override
        public V get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException,
                TimeoutException
        {
            synchronized (mutex) {
                while (!done) unit.timedWait(mutex, timeout);
                if (error != null) throw error;
                return value;
            }
        }
    };
}
 
Example 11
Source File: Promise.java    From jawampa with Apache License 2.0 4 votes vote down vote up
public java.util.concurrent.Future<V> getFuture() {
    return new Future<V>() {
        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }

        @Override
        public boolean isDone() {
            synchronized (mutex) {
                return done;
            }
        }

        @Override
        public V get() throws InterruptedException, ExecutionException {
            synchronized (mutex) {
                while (!done) mutex.wait();
                if (error != null) throw error;
                return value;
            }
        }

        @Override
        public V get(long timeout, TimeUnit unit)
                throws InterruptedException, ExecutionException,
                TimeoutException
        {
            synchronized (mutex) {
                while (!done) unit.timedWait(mutex, timeout);
                if (error != null) throw error;
                return value;
            }
        }
    };
}
 
Example 12
Source File: Flag.java    From document-viewer with GNU General Public License v3.0 3 votes vote down vote up
/**
 * Waits for flag changes.
 *
 * @param unit
 *          time unit
 * @param timeout
 *          timeout to wait
 * @return the flag value
 */
public synchronized boolean waitFor(final TimeUnit unit, final long timeout) {
  try {
    unit.timedWait(this, timeout);
  } catch (final InterruptedException ex) {
    Thread.interrupted();
  }
  return get();
}