@Override protected void afterExecute(Thread thread,Runnable r, Throwable t) { super.afterExecute(thread,r, t); completedTasksCount.incrementAndGet(); }
/** * {@inheritDoc} */ public List<Runnable> shutdownNow() { synchronized (statelock) { List<Runnable> drained = new ArrayList<Runnable>(); if (running) { running = false; drain(getQueue(), drained); for (Runnable task : drained) { onTaskDequeued(task); } poisonAll(); //try to interrupt their current work so they can get their poison fast for (Worker w : workers.keySet()) { w.t.interrupt(); } } return drained; } }
public AbstractThreadPool(ThreadPoolMonitoringProbe probe, String name, ThreadFactory threadFactory, int maxPoolSize){ if (maxPoolSize < 1) { throw new IllegalArgumentException("poolsize < 1"); } setName(name); corePoolSize = -1; this.maxPoolSize = maxPoolSize; this.probe = probe; this.threadFactory = threadFactory != null ? threadFactory : getDefaultThreadFactory(); }
public Thread newThread(Runnable r) { return new WorkerThreadImpl(AbstractThreadPool.this, getName() + "-WorkerThread(" + nextThreadId() + ")", r, getInitialByteBufferSize()); } };
/** * must hold statelock while calling this method. * @param wt */ protected void startWorker(Worker wt) { final Thread thread = threadFactory.newThread(wt); thread.setName(getName() + "(" + nextThreadId() + ")"); thread.setUncaughtExceptionHandler(this); thread.setPriority(getPriority()); thread.setDaemon(true); if (thread instanceof WorkerThreadImpl) { final WorkerThreadImpl workerThread = (WorkerThreadImpl) thread; workerThread.setByteBufferType(getByteBufferType()); workerThread.setInitialByteBufferSize(getInitialByteBufferSize()); } wt.t = thread; workers.put(wt, System.currentTimeMillis()); wt.t.start(); }
protected void doWork(){ final Thread t_=t; while(true) { try { Thread.interrupted(); final Runnable r = getTask(); if (r == poison || r == null){ return; } onTaskDequeued(r); Throwable error = null; try { beforeExecute(t_, r); //inside try. to ensure balance r.run(); onTaskCompletedEvent(r); } catch(Throwable throwable) { error = throwable; } finally { afterExecute(t_, r, error); } } catch (Throwable throwable) { } } }
protected void poisonAll() { int size = Math.max(maxPoolSize, workers.size()) * 4 / 3; final Queue<Runnable> q = getQueue(); while (size-- > 0) { q.offer(poison); } }
/** * Method is called by a thread pool, when new task could not be added * to a task queue, because task queue is full. * throws {@link RejectedExecutionException} */ protected void onTaskQueueOverflow() { if (probe != null){ probe.onTaskQueueOverflowEvent(name); } throw new RejectedExecutionException( "The thread pool's task queue is full, limit: "+getMaxQueuedTasksCount()); }
/** * Sets the {@link ThreadPoolConfig} * @param config * @return returns {@link GrizzlyExecutorService} */ public GrizzlyExecutorService reconfigure(ThreadPoolConfig config) { synchronized (statelock) { //TODO: only create new pool if old one cant be runtime config // for the needed state change(s). final ExtendedThreadPool oldpool = this.pool; if (config.getQueue() == oldpool.getQueue()) { config.setQueue(null); } setImpl(config); AbstractThreadPool.drain(oldpool.getQueue(), this.pool.getQueue()); oldpool.shutdown(); } return this; }