Friday, March 13, 2015

InterruptedException

java.lang.InterruptedException

InterruptedException is described in the javadoc comments as:

Thrown when a thread is waiting, sleeping, or otherwise paused for a long time and another thread interrupts it using the interrupt method in class Thread.
author: Frank Yellin version: 1.15, 12/19/03 see: java.lang.Object#wait() see: java.lang.Object#wait(long) see: java.lang.Object#wait(long, int) see: java.lang.Thread#sleep(long) see: java.lang.Thread#interrupt() see: java.lang.Thread#interrupted() since: JDK1.0

Where is this exception thrown?

Following, is a list of exception messages cross-referenced to the source code responsible for throwing them. Click on the method link to view the code and see how the exception is thrown.

How is this exception thrown?

The following sub-sections identify where this exception is thrown, and how (or why) the code is throwing the exception.

Any source code quoted in this section is subject to the Java Research License unless stated otherwise.

com.sun.corba.se.impl.orbutil.concurrent.CondVar.await()

/** 
   * Wait for notification. This operation at least momentarily
   * releases the mutex. The mutex is always held upon return, 
   * even if interrupted.
   * @exception InterruptedException if the thread was interrupted
   * before or during the wait. However, if the thread is interrupted
   * after the wait but during mutex re-acquisition, the interruption 
   * is ignored, while still ensuring
   * that the currentThread's interruption state stays true, so can
   * be probed by callers.
   **/
public void await() throws InterruptedException {
    int count = 0;
    if (Thread.interrupted()) throw new InterruptedException();
    try {
        if (debug_) ORBUtility.dprintTrace(this, 'await enter');
        synchronized (this) {
            count = releaseMutex();
            try {
                wait();
            } catch (InterruptedException ex) {
                notify();
                throw ex;
            }
        }
    } finally {
        boolean interrupted = false;
        for (; ; ) {
            try {
                acquireMutex(count);
                break;
            } catch (InterruptedException ex) {
                interrupted = true;
            }
        }
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
        if (debug_) ORBUtility.dprintTrace(this, 'await exit');
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.CondVar.timedwait(long)

/**
    * Wait for at most msecs for notification. 
    * This operation at least momentarily
    * releases the mutex. The mutex is always held upon return, 
    * even if interrupted.
    * @param msecs The time to wait. A value less than or equal to zero 
    * causes a momentarily release
    * and re-acquire of the mutex, and always returns false.
    * @return false if at least msecs have elapsed
    * upon resumption; else true. A 
    * false return does NOT necessarily imply that the thread was
    * not notified. For example, it might have been notified 
    * after the time elapsed but just before resuming.
    * @exception InterruptedException if the thread was interrupted
    * before or during the wait.
    **/
public boolean timedwait(long msecs) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    boolean success = false;
    int count = 0;
    try {
        if (debug_) ORBUtility.dprintTrace(this, 'timedwait enter');
        synchronized (this) {
            count = releaseMutex();
            try {
                if (msecs > 0) {
                    long start = System.currentTimeMillis();
                    wait(msecs);
                    success = System.currentTimeMillis() - start <= msecs;
                }
            } catch (InterruptedException ex) {
                notify();
                throw ex;
            }
        }
    } finally {
        boolean interrupted = false;
        for (; ; ) {
            try {
                acquireMutex(count);
                break;
            } catch (InterruptedException ex) {
                interrupted = true;
            }
        }
        if (interrupted) {
            Thread.currentThread().interrupt();
        }
        if (debug_) ORBUtility.dprintTrace(this, 'timedwait exit');
    }
    return success;
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.DebugMutex.acquire()

public void acquire() throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        Thread thr = Thread.currentThread();
        if (holder_ == thr) throw new INTERNAL('Attempt to acquire Mutex by thread holding the Mutex');
        try {
            while (inuse_) wait();
            inuse_ = true;
            holder_ = Thread.currentThread();
        } catch (InterruptedException ex) {
            notify();
            throw ex;
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.DebugMutex.attempt(long)

public boolean attempt(long msecs) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        Thread thr = Thread.currentThread();
        if (!inuse_) {
            inuse_ = true;
            holder_ = thr;
            return true;
        } else if (msecs <= 0) return false; else {
            long waitTime = msecs;
            long start = System.currentTimeMillis();
            try {
                for (; ; ) {
                    wait(waitTime);
                    if (!inuse_) {
                        inuse_ = true;
                        holder_ = thr;
                        return true;
                    } else {
                        waitTime = msecs - (System.currentTimeMillis() - start);
                        if (waitTime <= 0) return false;
                    }
                }
            } catch (InterruptedException ex) {
                notify();
                throw ex;
            }
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.Mutex.acquire()

public void acquire() throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        try {
            while (inuse_) wait();
            inuse_ = true;
        } catch (InterruptedException ex) {
            notify();
            throw ex;
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.Mutex.attempt(long)

public boolean attempt(long msecs) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        if (!inuse_) {
            inuse_ = true;
            return true;
        } else if (msecs <= 0) return false; else {
            long waitTime = msecs;
            long start = System.currentTimeMillis();
            try {
                for (; ; ) {
                    wait(waitTime);
                    if (!inuse_) {
                        inuse_ = true;
                        return true;
                    } else {
                        waitTime = msecs - (System.currentTimeMillis() - start);
                        if (waitTime <= 0) return false;
                    }
                }
            } catch (InterruptedException ex) {
                notify();
                throw ex;
            }
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.ReentrantMutex.acquire()

public void acquire() throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        try {
            if (debug) ORBUtility.dprintTrace(this, 'acquire enter: holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
            Thread thr = Thread.currentThread();
            if (holder_ != thr) {
                try {
                    while (counter_ > 0) wait();
                    if (counter_ != 0) throw new INTERNAL('counter not 0 when first acquiring mutex');
                    holder_ = thr;
                } catch (InterruptedException ex) {
                    notify();
                    throw ex;
                }
            }
            counter_++;
        } finally {
            if (debug) ORBUtility.dprintTrace(this, 'acquire exit: holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.ReentrantMutex.acquireAll(int)

void acquireAll(int count) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        try {
            if (debug) ORBUtility.dprintTrace(this, 'acquireAll enter: count=' + count + ' holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
            Thread thr = Thread.currentThread();
            if (holder_ == thr) {
                throw new INTERNAL('Cannot acquireAll while holding the mutex');
            } else {
                try {
                    while (counter_ > 0) wait();
                    if (counter_ != 0) throw new INTERNAL('counter not 0 when first acquiring mutex');
                    holder_ = thr;
                } catch (InterruptedException ex) {
                    notify();
                    throw ex;
                }
            }
            counter_ = count;
        } finally {
            if (debug) ORBUtility.dprintTrace(this, 'acquireAll exit: count=' + count + ' holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.corba.se.impl.orbutil.concurrent.ReentrantMutex.attempt(long)

public boolean attempt(long msecs) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    synchronized (this) {
        try {
            if (debug) ORBUtility.dprintTrace(this, 'attempt enter: msecs=' + msecs + ' holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
            Thread thr = Thread.currentThread();
            if (counter_ == 0) {
                holder_ = thr;
                counter_ = 1;
                return true;
            } else if (msecs <= 0) {
                return false;
            } else {
                long waitTime = msecs;
                long start = System.currentTimeMillis();
                try {
                    for (; ; ) {
                        wait(waitTime);
                        if (counter_ == 0) {
                            holder_ = thr;
                            counter_ = 1;
                            return true;
                        } else {
                            waitTime = msecs - (System.currentTimeMillis() - start);
                            if (waitTime <= 0) return false;
                        }
                    }
                } catch (InterruptedException ex) {
                    notify();
                    throw ex;
                }
            }
        } finally {
            if (debug) ORBUtility.dprintTrace(this, 'attempt exit: ' + ' holder_=' + ORBUtility.getThreadName(holder_) + ' counter_=' + counter_);
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.CyclicBarrier.dowait(boolean, long)

/**
     * Main barrier code, covering the various policies.
     */
private int dowait(boolean timed, long nanos) throws InterruptedException, BrokenBarrierException, TimeoutException {
    final ReentrantLock lock = this.lock;
    lock.lock();
    try {
        final Generation g = generation;
        if (g.broken) throw new BrokenBarrierException();
        if (Thread.interrupted()) {
            breakBarrier();
            throw new InterruptedException();
        }
        int index = --count;
        if (index == 0) {
            boolean ranAction = false;
            try {
                final Runnable command = barrierCommand;
                if (command != null) command.run();
                ranAction = true;
                nextGeneration();
                return 0;
            } finally {
                if (!ranAction) breakBarrier();
            }
        }
        for (; ; ) {
            try {
                if (!timed) trip.await(); else if (nanos > 0L) nanos = trip.awaitNanos(nanos);
            } catch (InterruptedException ie) {
                if (g == generation && !g.broken) {
                    breakBarrier();
                    throw ie;
                } else {
                    Thread.currentThread().interrupt();
                }
            }
            if (g.broken) throw new BrokenBarrierException();
            if (g != generation) return index;
            if (timed && nanos <= 0L) {
                breakBarrier();
                throw new TimeoutException();
            }
        }
    } finally {
        lock.unlock();
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.Exchanger.exchange(V)

/**
     * Waits for another thread to arrive at this exchange point (unless
     * it is {@link Thread#interrupt interrupted}),
     * and then transfers the given object to it, receiving its object
     * in return.
     * If another thread is already waiting at the exchange point then
     * it is resumed for thread scheduling purposes and receives the object
     * passed in by the current thread. The current thread returns immediately,
     * receiving the object passed to the exchange by that other thread.
     * If no other thread is already waiting at the exchange then the 
     * current thread is disabled for thread scheduling purposes and lies
     * dormant until one of two things happens:
     * <ul>
     * <li>Some other thread enters the exchange; or
     * <li>Some other thread {@link Thread#interrupt interrupts} the current
     * thread.
     * </ul>
     * If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or 
     * <li>is {@link Thread#interrupt interrupted} while waiting
     * for the exchange, 
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's 
     * interrupted status is cleared. 
     * @param x the object to exchange
     * @return the object provided by the other thread.
     * @throws InterruptedException if current thread was interrupted 
     * while waiting
     **/
public V exchange(V x) throws InterruptedException {
    if (!Thread.interrupted()) {
        Object v = doExchange(x == null ? NULL_ITEM : x, false, 0);
        if (v == NULL_ITEM) return null;
        if (v != CANCEL) return (V) v;
        Thread.interrupted();
    }
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.Exchanger.exchange(V, long, TimeUnit)

/**
     * Waits for another thread to arrive at this exchange point (unless
     * it is {@link Thread#interrupt interrupted}, or the specified waiting
     * time elapses),
     * and then transfers the given object to it, receiving its object
     * in return.
     * If another thread is already waiting at the exchange point then
     * it is resumed for thread scheduling purposes and receives the object
     * passed in by the current thread. The current thread returns immediately,
     * receiving the object passed to the exchange by that other thread.
     * If no other thread is already waiting at the exchange then the 
     * current thread is disabled for thread scheduling purposes and lies
     * dormant until one of three things happens:
     * <ul>
     * <li>Some other thread enters the exchange; or
     * <li>Some other thread {@link Thread#interrupt interrupts} the current
     * thread; or
     * <li>The specified waiting time elapses.
     * </ul>
     * If the current thread:
     * <ul>
     * <li>has its interrupted status set on entry to this method; or 
     * <li>is {@link Thread#interrupt interrupted} while waiting
     * for the exchange, 
     * </ul>
     * then {@link InterruptedException} is thrown and the current thread's 
     * interrupted status is cleared. 
     * If the specified waiting time elapses then {@link TimeoutException}
     * is thrown.
     * If the time is 
     * less than or equal to zero, the method will not wait at all.
     * @param x the object to exchange
     * @param timeout the maximum time to wait
     * @param unit the time unit of the <tt>timeout</tt> argument.
     * @return the object provided by the other thread.
     * @throws InterruptedException if current thread was interrupted
     * while waiting
     * @throws TimeoutException if the specified waiting time elapses before
     * another thread enters the exchange.
     **/
public V exchange(V x, long timeout, TimeUnit unit) throws InterruptedException, TimeoutException {
    if (!Thread.interrupted()) {
        Object v = doExchange(x == null ? NULL_ITEM : x, true, unit.toNanos(timeout));
        if (v == NULL_ITEM) return null;
        if (v != CANCEL) return (V) v;
        if (!Thread.interrupted()) throw new TimeoutException();
    }
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.SynchronousQueue.offer(E, long, TimeUnit)

/**
     * Inserts the specified element into this queue, waiting if necessary
     * up to the specified wait time for another thread to receive it.
     * @param o the element to add
     * @param timeout how long to wait before giving up, in units of
     * <tt>unit</tt>
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
     * <tt>timeout</tt> parameter
     * @return <tt>true</tt> if successful, or <tt>false</tt> if
     * the specified waiting time elapses before a consumer appears.
     * @throws InterruptedException if interrupted while waiting.
     * @throws NullPointerException if the specified element is <tt>null</tt>.
     */
public boolean offer(E o, long timeout, TimeUnit unit) throws InterruptedException {
    if (o == null) throw new NullPointerException();
    long nanos = unit.toNanos(timeout);
    final ReentrantLock qlock = this.qlock;
    for (; ; ) {
        Node node;
        boolean mustWait;
        if (Thread.interrupted()) throw new InterruptedException();
        qlock.lock();
        try {
            node = waitingConsumers.deq();
            if ((mustWait = (node == null))) node = waitingProducers.enq(o);
        } finally {
            qlock.unlock();
        }
        if (mustWait) return node.waitForTake(nanos); else if (node.setItem(o)) return true;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.SynchronousQueue.poll(long, TimeUnit)

/**
     * Retrieves and removes the head of this queue, waiting
     * if necessary up to the specified wait time, for another thread
     * to insert it.
     * @param timeout how long to wait before giving up, in units of
     * <tt>unit</tt>
     * @param unit a <tt>TimeUnit</tt> determining how to interpret the
     * <tt>timeout</tt> parameter
     * @return the head of this queue, or <tt>null</tt> if the
     * specified waiting time elapses before an element is present.
     * @throws InterruptedException if interrupted while waiting.
     */
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
    long nanos = unit.toNanos(timeout);
    final ReentrantLock qlock = this.qlock;
    for (; ; ) {
        Node node;
        boolean mustWait;
        if (Thread.interrupted()) throw new InterruptedException();
        qlock.lock();
        try {
            node = waitingProducers.deq();
            if ((mustWait = (node == null))) node = waitingConsumers.enq(null);
        } finally {
            qlock.unlock();
        }
        if (mustWait) {
            Object x = node.waitForPut(nanos);
            return (E) x;
        } else {
            Object x = node.getItem();
            if (x != null) return (E) x;
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.SynchronousQueue.put(E)

/**
     * Adds the specified element to this queue, waiting if necessary for
     * another thread to receive it.
     * @param o the element to add
     * @throws InterruptedException if interrupted while waiting.
     * @throws NullPointerException if the specified element is <tt>null</tt>.
     */
public void put(E o) throws InterruptedException {
    if (o == null) throw new NullPointerException();
    final ReentrantLock qlock = this.qlock;
    for (; ; ) {
        Node node;
        boolean mustWait;
        if (Thread.interrupted()) throw new InterruptedException();
        qlock.lock();
        try {
            node = waitingConsumers.deq();
            if ((mustWait = (node == null))) node = waitingProducers.enq(o);
        } finally {
            qlock.unlock();
        }
        if (mustWait) {
            node.waitForTake();
            return;
        } else if (node.setItem(o)) return;
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.SynchronousQueue.take()

/**
     * Retrieves and removes the head of this queue, waiting if necessary
     * for another thread to insert it.
     * @throws InterruptedException if interrupted while waiting.
     * @return the head of this queue
     */
public E take() throws InterruptedException {
    final ReentrantLock qlock = this.qlock;
    for (; ; ) {
        Node node;
        boolean mustWait;
        if (Thread.interrupted()) throw new InterruptedException();
        qlock.lock();
        try {
            node = waitingProducers.deq();
            if ((mustWait = (node == null))) node = waitingConsumers.enq(null);
        } finally {
            qlock.unlock();
        }
        if (mustWait) {
            Object x = node.waitForPut();
            return (E) x;
        } else {
            Object x = node.getItem();
            if (x != null) return (E) x;
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireInterruptibly(int)

/**
     * Acquires in exclusive mode, aborting if interrupted.
     * Implemented by first checking interrupt status, then invoking
     * at least once {@link #tryAcquire}, returning on
     * success.  Otherwise the thread is queued, possibly repeatedly
     * blocking and unblocking, invoking {@link #tryAcquire}
     * until success or the thread is interrupted.  This method can be
     * used to implement method {@link Lock#lockInterruptibly}
     * @param arg the acquire argument.
     * This value is conveyed to {@link #tryAcquire} but is
     * otherwise uninterpreted and can represent anything
     * you like.
     * @throws InterruptedException if the current thread is interrupted
     */
public final void acquireInterruptibly(int arg) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    if (!tryAcquire(arg)) doAcquireInterruptibly(arg);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(int)

/**
     * Acquires in shared mode, aborting if interrupted.  Implemented
     * by first checking interrupt status, then invoking at least once
     * {@link #tryAcquireShared}, returning on success.  Otherwise the
     * thread is queued, possibly repeatedly blocking and unblocking,
     * invoking {@link #tryAcquireShared} until success or the thread
     * is interrupted.  
     * @param arg the acquire argument.
     * This value is conveyed to {@link #tryAcquireShared} but is
     * otherwise uninterpreted and can represent anything
     * you like.
     * @throws InterruptedException if the current thread is interrupted
     */
public final void acquireSharedInterruptibly(int arg) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    if (tryAcquireShared(arg) < 0) doAcquireSharedInterruptibly(arg);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireInterruptibly(int)

/** 
     * Acquire in exclusive interruptible mode
     * @param arg the acquire argument
     */
private void doAcquireInterruptibly(int arg) throws InterruptedException {
    final Node node = addWaiter(Node.EXCLUSIVE);
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head && tryAcquire(arg)) {
                setHead(node);
                p.next = null;
                return;
            }
            if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) break;
        }
    } catch (RuntimeException ex) {
        cancelAcquire(node);
        throw ex;
    }
    cancelAcquire(node);
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireNanos(int, long)

/** 
     * Acquire in exclusive timed mode
     * @param arg the acquire argument
     * @param nanosTimeout max wait time
     * @return true if acquired
     */
private boolean doAcquireNanos(int arg, long nanosTimeout) throws InterruptedException {
    long lastTime = System.nanoTime();
    final Node node = addWaiter(Node.EXCLUSIVE);
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head && tryAcquire(arg)) {
                setHead(node);
                p.next = null;
                return true;
            }
            if (nanosTimeout <= 0) {
                cancelAcquire(node);
                return false;
            }
            if (shouldParkAfterFailedAcquire(p, node)) {
                LockSupport.parkNanos(nanosTimeout);
                if (Thread.interrupted()) break;
                long now = System.nanoTime();
                nanosTimeout -= now - lastTime;
                lastTime = now;
            }
        }
    } catch (RuntimeException ex) {
        cancelAcquire(node);
        throw ex;
    }
    cancelAcquire(node);
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(int)

/** 
     * Acquire in shared interruptible mode
     * @param arg the acquire argument
     */
private void doAcquireSharedInterruptibly(int arg) throws InterruptedException {
    final Node node = addWaiter(Node.SHARED);
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head) {
                int r = tryAcquireShared(arg);
                if (r >= 0) {
                    setHeadAndPropagate(node, r);
                    p.next = null;
                    return;
                }
            }
            if (shouldParkAfterFailedAcquire(p, node) && parkAndCheckInterrupt()) break;
        }
    } catch (RuntimeException ex) {
        cancelAcquire(node);
        throw ex;
    }
    cancelAcquire(node);
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedNanos(int, long)

/** 
     * Acquire in shared timed mode
     * @param arg the acquire argument
     * @param nanosTimeout max wait time
     * @return true if acquired
     */
private boolean doAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException {
    long lastTime = System.nanoTime();
    final Node node = addWaiter(Node.SHARED);
    try {
        for (; ; ) {
            final Node p = node.predecessor();
            if (p == head) {
                int r = tryAcquireShared(arg);
                if (r >= 0) {
                    setHeadAndPropagate(node, r);
                    p.next = null;
                    return true;
                }
            }
            if (nanosTimeout <= 0) {
                cancelAcquire(node);
                return false;
            }
            if (shouldParkAfterFailedAcquire(p, node)) {
                LockSupport.parkNanos(nanosTimeout);
                if (Thread.interrupted()) break;
                long now = System.nanoTime();
                nanosTimeout -= now - lastTime;
                lastTime = now;
            }
        }
    } catch (RuntimeException ex) {
        cancelAcquire(node);
        throw ex;
    }
    cancelAcquire(node);
    throw new InterruptedException();
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireNanos(int, long)

/**
     * Attempts to acquire in exclusive mode, aborting if interrupted,
     * and failing if the given timeout elapses.  Implemented by first
     * checking interrupt status, then invoking at least once {@link
     * #tryAcquire}, returning on success.  Otherwise, the thread is
     * queued, possibly repeatedly blocking and unblocking, invoking
     * {@link #tryAcquire} until success or the thread is interrupted
     * or the timeout elapses.  This method can be used to implement
     * method {@link Lock#tryLock(long, TimeUnit)}.
     * @param arg the acquire argument.
     * This value is conveyed to {@link #tryAcquire} but is
     * otherwise uninterpreted and can represent anything
     * you like.
     * @param nanosTimeout the maximum number of nanoseconds to wait
     * @return true if acquired; false if timed out
     * @throws InterruptedException if the current thread is interrupted
     */
public final boolean tryAcquireNanos(int arg, long nanosTimeout) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    return tryAcquire(arg) || doAcquireNanos(arg, nanosTimeout);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

java.util.concurrent.locks.AbstractQueuedSynchronizer.tryAcquireSharedNanos(int, long)

/**
     * Attempts to acquire in shared mode, aborting if interrupted, and
     * failing if the given timeout elapses.  Implemented by first
     * checking interrupt status, then invoking at least once {@link
     * #tryAcquireShared}, returning on success.  Otherwise, the
     * thread is queued, possibly repeatedly blocking and unblocking,
     * invoking {@link #tryAcquireShared} until success or the thread
     * is interrupted or the timeout elapses.
     * @param arg the acquire argument.
     * This value is conveyed to {@link #tryAcquireShared} but is
     * otherwise uninterpreted and can represent anything
     * you like.
     * @param nanosTimeout the maximum number of nanoseconds to wait
     * @return true if acquired; false if timed out
     * @throws InterruptedException if the current thread is interrupted
     */
public final boolean tryAcquireSharedNanos(int arg, long nanosTimeout) throws InterruptedException {
    if (Thread.interrupted()) throw new InterruptedException();
    return tryAcquireShared(arg) >= 0 || doAcquireSharedNanos(arg, nanosTimeout);
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.jmx.snmp.daemon.SnmpAdaptorServer.doBind()

/**
     * Creates the datagram socket.
     */
protected void doBind() throws CommunicationException, InterruptedException {
    try {
        synchronized (this) {
            socket = new DatagramSocket(port, address);
        }
        dbgTag = makeDebugTag();
    } catch (SocketException e) {
        if (e.getMessage().equals(InterruptSysCallMsg)) throw new InterruptedException(e.toString()); else {
            if (isDebugOn()) {
                debug('doBind', 'cannot bind on port ' + port);
            }
            throw new CommunicationException(e);
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.jmx.snmp.daemon.SnmpAdaptorServer.doReceive()

/**
     * Reads a packet from the datagram socket and creates a request 
     * handler which decodes and processes the request.
     */
protected void doReceive() throws CommunicationException, InterruptedException {
    try {
        packet = new DatagramPacket(new byte[bufferSize], bufferSize);
        socket.receive(packet);
        int state = getState();
        if (state != ONLINE) {
            if (isTraceOn()) {
                trace('doReceive', 'received a message but state not online, returning.');
            }
            return;
        }
        createSnmpRequestHandler(this, servedClientCount, socket, packet, root, mibs, ipacl, pduFactory, userDataFactory, topMBS, objectName);
    } catch (SocketException e) {
        if (e.getMessage().equals(InterruptSysCallMsg)) throw new InterruptedException(e.toString()); else throw new CommunicationException(e);
    } catch (InterruptedIOException e) {
        throw new InterruptedException(e.toString());
    } catch (CommunicationException e) {
        throw e;
    } catch (Exception e) {
        throw new CommunicationException(e);
    }
    if (isTraceOn()) {
        trace('doReceive', 'received a message');
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

com.sun.jmx.snmp.daemon.CommunicatorServer.waitForStart(long)

/**
     * Waits until the communicator is started or timeout expires.
     * @param timeout Time in ms to wait for the connector to start.
     *        If <code>timeout</code> is positive, wait for at most
     *        the specified time. An infinite timeout can be specified
     *        by passing a <code>timeout</code> value equals 
     *        <code>Long.MAX_VALUE</code>. In that case the method
     *        will wait until the connector starts or fails to start. 
     *        If timeout is negative or zero, returns as soon as possible 
     *        without waiting.
     * @exception CommunicationException if the connectors fails to start.
     * @exception InterruptedException if the thread is interrupted or the
     *            timeout expires.
     */
private void waitForStart(long timeout) throws CommunicationException, InterruptedException {
    if (isTraceOn()) trace('waitForStart', 'Timeout=' + timeout + ' ; current state = ' + getStateString());
    final long startTime = System.currentTimeMillis();
    synchronized (stateLock) {
        while (state == STARTING) {
            final long elapsed = System.currentTimeMillis() - startTime;
            final long remainingTime = timeout - elapsed;
            if (remainingTime < 0) {
                if (isTraceOn()) trace('waitForStart', 'timeout < 0, return without wait');
                throw new InterruptedException('Timeout expired');
            }
            try {
                stateLock.wait(remainingTime);
            } catch (InterruptedException e) {
                if (isTraceOn()) trace('waitForStart', 'wait interrupted');
                if (state != ONLINE) throw e;
            }
        }
        if (state == ONLINE) {
            if (isTraceOn()) trace('waitForStart', 'started');
            return;
        } else if (startException instanceof CommunicationException) {
            throw (CommunicationException) startException;
        } else if (startException instanceof InterruptedException) {
            throw (InterruptedException) startException;
        } else if (startException != null) {
            throw new CommunicationException(startException, 'Failed to start: ' + startException);
        } else {
            throw new CommunicationException('Failed to start: state is ' + getStringForState(state));
        }
    }
}

Source: "Java SE Downloads: Java SE 6 JDK Source Code", at: http://www.oracle.com/technetwork/java/javase/downloads/index.html

No comments:

Post a Comment