@Override protected void doStart() throws Exception { if (_executor.isShutdown()) throw new IllegalStateException("This thread pool is not restartable"); for (int i = 0; i < _minThreads; ++i) _executor.prestartCoreThread(); _tryExecutor = new ReservedThreadExecutor(this, _reservedThreads); addBean(_tryExecutor); super.doStart(); }
/** * @return the number of busy threads in the pool */ @ManagedAttribute("number of busy threads in the pool") public int getBusyThreads() { int reserved = _tryExecutor instanceof ReservedThreadExecutor ? ((ReservedThreadExecutor)_tryExecutor).getAvailable() : 0; return getThreads() - getIdleThreads() - reserved; }
/** * @return number of reserved threads or -1 to indicate that the number is heuristically determined * @see #setReservedThreads(int) */ @ManagedAttribute("the number of reserved threads in the pool") public int getReservedThreads() { if (isStarted()) return getBean(ReservedThreadExecutor.class).getCapacity(); return _reservedThreads; }
/** * Set the idle timeout for shrinking the reserved thread pool * @param idleTime Time to wait before shrinking, or 0 for no timeout. * @param idleTimeUnit Time units for idle timeout */ public void setIdleTimeout(long idleTime, TimeUnit idleTimeUnit) { if (isRunning()) throw new IllegalStateException(); _idleTime = idleTime; _idleTimeUnit = idleTimeUnit; }
@Override public void doStart() throws Exception { _lease = ThreadPoolBudget.leaseFrom(getExecutor(),this,_capacity); super.doStart(); }
/** * @param executor The executor to use to obtain threads * @param capacity The number of threads to preallocate. If less than 0 then capacity * is calculated based on a heuristic from the number of available processors and * thread pool size. */ public ReservedThreadExecutor(Executor executor,int capacity) { _executor = executor; _capacity = reservedThreads(executor,capacity); _stack = new ConcurrentLinkedDeque<>(); LOG.debug("{}",this); }
/** * @param task The task to run * @return True iff a reserved thread was available and has been assigned the task to run. */ @Override public boolean tryExecute(Runnable task) { if (LOG.isDebugEnabled()) LOG.debug("{} tryExecute {}",this ,task); if (task==null) return false; ReservedThread thread = _stack.pollFirst(); if (thread==null) { if (task!=STOP) startReservedThread(); return false; } int size = _size.decrementAndGet(); thread.offer(task); if (size==0 && task!=STOP) startReservedThread(); return true; }
@Override public void run() while (isRunning())
@Override public void doStart() throws Exception { _lease = ThreadPoolBudget.leaseFrom(getExecutor(),this,_capacity); super.doStart(); }
/** * @param executor The executor to use to obtain threads * @param capacity The number of threads to preallocate. If less than 0 then capacity * is calculated based on a heuristic from the number of available processors and * thread pool size. */ public ReservedThreadExecutor(Executor executor,int capacity) { _executor = executor; _capacity = reservedThreads(executor,capacity); _stack = new ConcurrentLinkedDeque<>(); LOG.debug("{}",this); }
/** * @param task The task to run * @return True iff a reserved thread was available and has been assigned the task to run. */ @Override public boolean tryExecute(Runnable task) { if (LOG.isDebugEnabled()) LOG.debug("{} tryExecute {}",this ,task); if (task==null) return false; ReservedThread thread = _stack.pollFirst(); if (thread==null) { if (task!=STOP) startReservedThread(); return false; } int size = _size.decrementAndGet(); thread.offer(task); if (size==0 && task!=STOP) startReservedThread(); return true; }
/** * Set the idle timeout for shrinking the reserved thread pool * @param idleTime Time to wait before shrinking, or 0 for no timeout. * @param idleTimeUnit Time units for idle timeout */ public void setIdleTimeout(long idleTime, TimeUnit idleTimeUnit) { if (isRunning()) throw new IllegalStateException(); _idleTime = idleTime; _idleTimeUnit = idleTimeUnit; }
/** * Get the number of reserved threads. * * @return number of reserved threads or or -1 for heuristically determined * @see #setReservedThreads */ @ManagedAttribute("the number of reserved threads in the pool") public int getReservedThreads() { if (isStarted()) return getBean(ReservedThreadExecutor.class).getCapacity(); return _reservedThreads; }
@Override protected void doStart() throws Exception { _tryExecutor = new ReservedThreadExecutor(this,_reservedThreads); addBean(_tryExecutor); super.doStart(); _threadsStarted.set(0); startThreads(_minThreads); }
/** * @return the number of busy threads in the pool */ @ManagedAttribute("number of busy threads in the pool") public int getBusyThreads() { int reserved = _tryExecutor instanceof ReservedThreadExecutor ? ((ReservedThreadExecutor)_tryExecutor).getAvailable() : 0; return getThreads() - getIdleThreads() - reserved; }
@Override public void run() while (isRunning())
/** * @return number of reserved threads or -1 to indicate that the number is heuristically determined * @see #setReservedThreads(int) */ @ManagedAttribute("the number of reserved threads in the pool") public int getReservedThreads() { if (isStarted()) return getBean(ReservedThreadExecutor.class).getCapacity(); return _reservedThreads; }
@Override protected void doStart() throws Exception { if (_executor.isShutdown()) throw new IllegalStateException("This thread pool is not restartable"); for (int i = 0; i < _minThreads; ++i) _executor.prestartCoreThread(); _tryExecutor = new ReservedThreadExecutor(this, _reservedThreads); addBean(_tryExecutor); super.doStart(); }