Beispiel #1
0
 /**
  * Waits for the channel to become terminated, giving up if the timeout is reached.
  *
  * @return whether the channel is terminated, as would be done by {@link #isTerminated()}.
  */
 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
   synchronized (lock) {
     long timeoutNanos = unit.toNanos(timeout);
     long endTimeNanos = System.nanoTime() + timeoutNanos;
     while (!terminated && (timeoutNanos = endTimeNanos - System.nanoTime()) > 0) {
       TimeUnit.NANOSECONDS.timedWait(lock, timeoutNanos);
     }
     return terminated;
   }
 }
  /**
   * Causes the current thread to wait until this instance acquires leadership unless the thread is
   * {@linkplain Thread#interrupt interrupted}, the specified waiting time elapses or the instance
   * is {@linkplain #close() closed}.
   *
   * <p>If this instance already is the leader then this method returns immediately with the value
   * {@code true}.
   *
   * <p>Otherwise the current thread becomes disabled for thread scheduling purposes and lies
   * dormant until one of four things happen:
   *
   * <ul>
   *   <li>This instance becomes the leader
   *   <li>Some other thread {@linkplain Thread#interrupt interrupts} the current thread
   *   <li>The specified waiting time elapses.
   *   <li>The instance is {@linkplain #close() closed}
   * </ul>
   *
   * <p>If the current thread:
   *
   * <ul>
   *   <li>has its interrupted status set on entry to this method; or
   *   <li>is {@linkplain Thread#interrupt interrupted} while waiting,
   * </ul>
   *
   * then {@link InterruptedException} is thrown and the current thread's interrupted status is
   * cleared.
   *
   * <p>If the specified waiting time elapses or the instance is {@linkplain #close() closed} then
   * the value {@code false} is returned. If the time is less than or equal to zero, the method will
   * not wait at all.
   *
   * @param timeout the maximum time to wait
   * @param unit the time unit of the {@code timeout} argument
   * @return {@code true} if the count reached zero and {@code false} if the waiting time elapsed
   *     before the count reached zero or the instances was closed
   * @throws InterruptedException if the current thread is interrupted while waiting
   */
  public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
    long waitNanos = TimeUnit.NANOSECONDS.convert(timeout, unit);

    synchronized (this) {
      while ((waitNanos > 0) && (state.get() == State.STARTED) && !hasLeadership.get()) {
        long startNanos = System.nanoTime();
        TimeUnit.NANOSECONDS.timedWait(this, waitNanos);
        long elapsed = System.nanoTime() - startNanos;
        waitNanos -= elapsed;
      }
    }
    return hasLeadership();
  }
Beispiel #3
0
 @Override
 public boolean awaitTermination(long timeout, TimeUnit unit) throws InterruptedException {
   long nanos = unit.toNanos(timeout);
   synchronized (lock) {
     while (true) {
       if (shutdown && runningTasks == 0) {
         return true;
       } else if (nanos <= 0) {
         return false;
       } else {
         long now = System.nanoTime();
         TimeUnit.NANOSECONDS.timedWait(lock, nanos);
         nanos -= System.nanoTime() - now; // subtract the actual time we waited
       }
     }
   }
 }
 /** {@inheritDoc} */
 @Override
 public void await(Object obj) throws InterruptedException, TimeoutException {
   long r = getRemains();
   TimeUnit.NANOSECONDS.timedWait(obj, r);
 }