예제 #1
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();
   }
 }
예제 #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) {
           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();
    }
  }
예제 #6
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();
 }
예제 #7
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();
 }
예제 #8
0
 /**
  * Causes the current thread to wait until the latch has counted down to zero, unless the thread
  * is {@linkplain Thread#interrupt interrupted}, or the specified waiting time elapses.
  *
  * <p>If the current count is zero then this method returns immediately with the value {@code
  * true}.
  *
  * <p>If the current count is greater than zero then the current thread becomes disabled for
  * thread scheduling purposes and lies dormant until one of three things happen:
  *
  * <ul>
  *   <li>The count reaches zero due to invocations of the {@link #countDown} method; or
  *   <li>Some other thread {@linkplain Thread#interrupt interrupts} the current thread; or
  *   <li>The specified waiting time elapses.
  * </ul>
  *
  * <p>If the count reaches zero then the method returns with the value {@code true}.
  *
  * <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 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
  * @throws InterruptedException if the current thread is interrupted while waiting
  */
 public boolean await(long timeout, TimeUnit unit) throws InterruptedException {
   return sync.tryAcquireSharedNanos(1, unit.toNanos(timeout));
 }