Пример #1
0
  /**
   * Wait until futures are complete or the supplied timeout is reached.
   *
   * @param timeout Maximum time to wait for futures to complete.
   * @param unit Unit of time for the timeout.
   * @param futures Futures to wait for.
   * @return True if all futures complete in time.
   */
  public boolean awaitAll(long timeout, TimeUnit unit, Future<?>... futures) {
    boolean complete;

    try {
      // 将timeout 转换为微秒
      long nanos = unit.toNanos(timeout);
      // 获取系统当前时间的微秒
      long time = System.nanoTime();

      for (Future<?> f : futures) {
        if (nanos < 0) return false;
        // 此处f的示例为Command
        f.get(nanos, TimeUnit.NANOSECONDS);
        long now = System.nanoTime();
        nanos -= now - time;
        time = now;
      }

      complete = true;
    } catch (TimeoutException e) {
      complete = false;
    } catch (Exception e) {
      throw new RedisCommandInterruptedException(e);
    }

    return complete;
  }
Пример #2
0
 /**
  * Retrieves and removes the head of this queue, waiting if necessary until an element with an
  * expired delay is available on this queue, or the specified wait time expires.
  *
  * @return the head of this queue, or <tt>null</tt> if the specified waiting time elapses before
  *     an element with an expired delay becomes available
  * @throws InterruptedException {@inheritDoc}
  */
 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
   long nanos = unit.toNanos(timeout);
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       E first = q.peek();
       if (first == null) {
         if (nanos <= 0) return null;
         else nanos = available.awaitNanos(nanos);
       } else {
         long delay = first.getDelay(TimeUnit.NANOSECONDS);
         if (delay <= 0) return q.poll();
         if (nanos <= 0) return null;
         if (nanos < delay || leader != null) nanos = available.awaitNanos(nanos);
         else {
           Thread thisThread = Thread.currentThread();
           leader = thisThread;
           try {
             long timeLeft = available.awaitNanos(delay);
             nanos -= delay - timeLeft;
           } finally {
             if (leader == thisThread) leader = null;
           }
         }
       }
     }
   } finally {
     if (leader == null && q.peek() != null) available.signal();
     lock.unlock();
   }
 }
Пример #3
0
 /**
  * Retrieves and removes the head of this queue, waiting if necessary until an element with an
  * expired delay is available on this queue, or the specified wait time expires.
  *
  * @return the head of this queue, or <tt>null</tt> if the specified waiting time elapses before
  *     an element with an expired delay becomes available
  * @throws InterruptedException {@inheritDoc}
  */
 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
   long nanos = unit.toNanos(timeout);
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       E first = q.peek();
       if (first == null) {
         if (nanos <= 0) return null;
         else nanos = available.awaitNanos(nanos);
       } else {
         long delay = first.getDelay(TimeUnit.NANOSECONDS);
         if (delay > 0) {
           if (nanos <= 0) return null;
           if (delay > nanos) delay = nanos;
           long timeLeft = available.awaitNanos(delay);
           nanos -= delay - timeLeft;
         } else {
           E x = q.poll();
           assert x != null;
           if (q.size() != 0) available.signalAll();
           return x;
         }
       }
     }
   } finally {
     lock.unlock();
   }
 }
  /** {@inheritDoc} */
  @Override
  public R get(long timeout, TimeUnit unit) throws IgniteCheckedException {
    A.ensure(timeout >= 0, "timeout cannot be negative: " + timeout);
    A.notNull(unit, "unit");

    try {
      return get0(unit.toNanos(timeout));
    } catch (InterruptedException e) {
      Thread.currentThread().interrupt();

      throw new IgniteInterruptedCheckedException(
          "Got interrupted while waiting for future to complete.", e);
    }
  }
 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
   long nanos = unit.toNanos(timeout);
   final ReentrantLock lock = this.lock;
   lock.lockInterruptibly();
   try {
     for (; ; ) {
       if (count != 0) {
         E x = extract();
         return x;
       }
       if (nanos <= 0) return null;
       try {
         nanos = notEmpty.awaitNanos(nanos);
       } catch (InterruptedException ie) {
         notEmpty.signal(); // propagate to non-interrupted thread
         throw ie;
       }
     }
   } finally {
     lock.unlock();
   }
 }
  /**
   * Inserts the specified element at the tail of this queue, waiting up to the specified wait time
   * for space to become available if the queue is full.
   *
   * @throws InterruptedException {@inheritDoc}
   * @throws NullPointerException {@inheritDoc}
   */
  public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {

    if (e == null) throw new NullPointerException();
    long nanos = unit.toNanos(timeout);
    final ReentrantLock lock = this.lock;
    lock.lockInterruptibly();
    try {
      for (; ; ) {
        if (count != items.length) {
          insert(e);
          return true;
        }
        if (nanos <= 0) return false;
        try {
          nanos = notFull.awaitNanos(nanos);
        } catch (InterruptedException ie) {
          notFull.signal(); // propagate to non-interrupted thread
          throw ie;
        }
      }
    } finally {
      lock.unlock();
    }
  }
Пример #7
0
 /**
  * Retrieves and removes the head of this queue, waiting if necessary up to the specified wait
  * time, for another thread to insert it.
  *
  * @return the head of this queue, or <tt>null</tt> if the specified waiting time elapses before
  *     an element is present.
  * @throws InterruptedException {@inheritDoc}
  */
 public E poll(long timeout, TimeUnit unit) throws InterruptedException {
   Object e = transferer.transfer(null, true, unit.toNanos(timeout));
   if (e != null || !Thread.interrupted()) return (E) e;
   throw new InterruptedException();
 }
Пример #8
0
 /**
  * Inserts the specified element into this queue, waiting if necessary up to the specified wait
  * time for another thread to receive it.
  *
  * @return <tt>true</tt> if successful, or <tt>false</tt> if the specified waiting time elapses
  *     before a consumer appears.
  * @throws InterruptedException {@inheritDoc}
  * @throws NullPointerException {@inheritDoc}
  */
 public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException {
   if (o == null) throw new NullPointerException();
   if (transferer.transfer(o, true, unit.toNanos(timeout)) != null) return true;
   if (!Thread.interrupted()) return false;
   throw new InterruptedException();
 }
 /**
  * Acquires the lock if it is not held by another thread within the given waiting time and the
  * current thread has not been {@link Thread#interrupt interrupted}.
  *
  * <p>Acquires the lock if it is not held by another thread and returns immediately with the value
  * <tt>true</tt>, setting the lock hold count to one. If this lock has been set to use a fair
  * ordering policy then an available lock <em>will not</em> be acquired if any other threads are
  * waiting for the lock. This is in contrast to the {@link #tryLock()} method. If you want a timed
  * <tt>tryLock</tt> that does permit barging on a fair lock then combine the timed and un-timed
  * forms together:
  *
  * <pre>if (lock.tryLock() || lock.tryLock(timeout, unit) ) { ... }
  * </pre>
  *
  * <p>If the current thread already holds this lock then the hold count is incremented by one and
  * the method returns <tt>true</tt>.
  *
  * <p>If the lock is held by another thread then the current thread becomes disabled for thread
  * scheduling purposes and lies dormant until one of three things happens:
  *
  * <ul>
  *   <li>The lock is acquired by the current thread; or
  *   <li>Some other thread {@link Thread#interrupt interrupts} the current thread; or
  *   <li>The specified waiting time elapses
  * </ul>
  *
  * <p>If the lock is acquired then the value <tt>true</tt> is returned and the lock hold count is
  * set to one.
  *
  * <p>If the current thread:
  *
  * <ul>
  *   <li>has its interrupted status set on entry to this method; or
  *   <li>is {@link Thread#interrupt interrupted} while acquiring the lock,
  * </ul>
  *
  * then {@link InterruptedException} is thrown and the current thread's interrupted status is
  * cleared.
  *
  * <p>If the specified waiting time elapses then the value <tt>false</tt> is returned. If the time
  * is less than or equal to zero, the method will not wait at all.
  *
  * <p>In this implementation, as this method is an explicit interruption point, preference is
  * given to responding to the interrupt over normal or reentrant acquisition of the lock, and over
  * reporting the elapse of the waiting time.
  *
  * @param timeout the time to wait for the lock
  * @param unit the time unit of the timeout argument
  * @return <tt>true</tt> if the lock was free and was acquired by the current thread, or the lock
  *     was already held by the current thread; and <tt>false</tt> if the waiting time elapsed
  *     before the lock could be acquired.
  * @throws InterruptedException if the current thread is interrupted
  * @throws NullPointerException if unit is null
  */
 public boolean tryLock(long timeout, TimeUnit unit) throws InterruptedException {
   return sync.tryLock(unit.toNanos(timeout));
 }