/** * Returns the thread keep-alive time, which is the amount of time * that threads in excess of the core pool size may remain * idle before being terminated. * * @param unit the desired time unit of the result * @return the time limit * @see #setKeepAliveTime */ public long getKeepAliveTime(TimeUnit unit) { return unit.convert(keepAliveTime, TimeUnit.NANOSECONDS); }
public long convert(long d, TimeUnit u) { return u.toNanos(d); } int excessNanos(long d, long m) { return (int)(d - (m*C2)); }
/** * Performs a <tt>Thread.sleep</tt> using this unit. * This is a convenience method that converts time arguments into the * form required by the <tt>Thread.sleep</tt> method. * @param timeout the maximum time to sleep. If less than * or equal to zero, do not sleep at all. * @throws InterruptedException if interrupted while sleeping. * @see java.lang.Thread#sleep */ public void sleep(long timeout) throws InterruptedException { if (timeout > 0) { long ms = toMillis(timeout); int ns = excessNanos(timeout, ms); Thread.sleep(ms, ns); } }
public Object poll(long timeout, TimeUnit unit) throws InterruptedException { Object x = null; int c = -1; long nanos = unit.toNanos(timeout); synchronized (takeLock) { long deadline = Utils.nanoTime() + nanos; for (;;) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); break; } if (nanos <= 0) return null; try { TimeUnit.NANOSECONDS.timedWait(takeLock, nanos); nanos = deadline - Utils.nanoTime(); } catch (InterruptedException ie) { takeLock.notify(); // propagate to a non-interrupted thread throw ie; } } } if (c == capacity) signalNotFull(); return x; }
private boolean attempt(long nanos) throws InterruptedException { if (state != 0) return true; if (nanos <= 0) { state = CANCEL; notify(); return false; } long deadline = Utils.nanoTime() + nanos; while (true) { TimeUnit.NANOSECONDS.timedWait(this, nanos); if (state != 0) return true; nanos = deadline - Utils.nanoTime(); if (nanos <= 0) { state = CANCEL; notify(); return false; } } }
public long convert(long d, TimeUnit u) { return u.toMillis(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toHours(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toMicros(d); } int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toDays(d); } int excessNanos(long d, long m) { return 0; }
public Object poll(long timeout, TimeUnit unit) throws InterruptedException { Object x = null; int c = -1; long nanos = unit.toNanos(timeout); synchronized (takeLock) { long deadline = Utils.nanoTime() + nanos; for (;;) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); break; } if (nanos <= 0) return null; try { TimeUnit.NANOSECONDS.timedWait(takeLock, nanos); nanos = deadline - Utils.nanoTime(); } catch (InterruptedException ie) { takeLock.notify(); // propagate to a non-interrupted thread throw ie; } } } if (c == capacity) signalNotFull(); return x; }
private boolean attempt(long nanos) throws InterruptedException { if (state != 0) return true; if (nanos <= 0) { state = CANCEL; notify(); return false; } long deadline = Utils.nanoTime() + nanos; while (true) { TimeUnit.NANOSECONDS.timedWait(this, nanos); if (state != 0) return true; nanos = deadline - Utils.nanoTime(); if (nanos <= 0) { state = CANCEL; notify(); return false; } } }
public long convert(long d, TimeUnit u) { return u.toMillis(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toHours(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toMinutes(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toMicros(d); } int excessNanos(long d, long m) { return (int)((d*C1) - (m*C2)); }
public long convert(long d, TimeUnit u) { return u.toSeconds(d); } int excessNanos(long d, long m) { return 0; }
public long convert(long d, TimeUnit u) { return u.toDays(d); } int excessNanos(long d, long m) { return 0; }
public Object poll(long timeout, TimeUnit unit) throws InterruptedException { Object x = null; int c = -1; long nanos = unit.toNanos(timeout); synchronized (takeLock) { long deadline = Utils.nanoTime() + nanos; for (;;) { if (count > 0) { x = extract(); synchronized (this) { c = count--; } if (c > 1) takeLock.notify(); break; } if (nanos <= 0) return null; try { TimeUnit.NANOSECONDS.timedWait(takeLock, nanos); nanos = deadline - Utils.nanoTime(); } catch (InterruptedException ie) { takeLock.notify(); // propagate to a non-interrupted thread throw ie; } } } if (c == capacity) signalNotFull(); return x; }