final int result; for (;;) { tailNext = tail.getNext(); if (tailNext instanceof TaskNode) { TaskNode tailNextTaskNode; tailNext = tail.getNext(); } while (tailNext instanceof TaskNode); if (tail.compareAndSetNext(tailNext, tailNextNext)) { PoolThreadNode consumerNode = (PoolThreadNode) tailNext; TaskNode node = new TaskNode(runnable); if (tail.compareAndSetNext(null, node)) {
doRunTask(((TaskNode) node).getAndClearTask()); continue; } else if (node instanceof PoolThreadNode) {
void completeTermination() { assert ! holdsLock(headLock) && ! holdsLock(tailLock); // be kind and un-interrupt the thread for the termination task Thread.interrupted(); final Runnable terminationTask = this.terminationTask; this.terminationTask = null; safeRun(terminationTask); // notify all waiters Waiter waiters = getAndSetTerminationWaiters(TERMINATE_COMPLETE_WAITER); while (waiters != null) { unpark(waiters.getThread()); waiters = waiters.getNext(); } tail.getAndSetNext(TERMINATE_COMPLETE); final ObjectInstance handle = this.handle; if (handle != null) { doPrivileged(new PrivilegedAction<Void>() { public Void run() { try { ManagementFactory.getPlatformMBeanServer().unregisterMBean(handle.getObjectName()); } catch (Throwable ignored) { } return null; } }, acc); } }
/** * The builder class for an {@code EnhancedQueueExecutor}. All the fields are initialized to sensible defaults for * a small thread pool. */ public static final class Builder { private ThreadFactory threadFactory = Executors.defaultThreadFactory(); private Runnable terminationTask = NullRunnable.getInstance(); private Executor handoffExecutor = DEFAULT_HANDLER; private Thread.UncaughtExceptionHandler exceptionHandler = JBossExecutors.loggingExceptionHandler(); private int coreSize = 16; private int maxSize = 64; private long keepAliveTime = 30; private TimeUnit keepAliveUnits = TimeUnit.SECONDS; private float growthResistance; private boolean allowCoreTimeOut; private int maxQueueSize = Integer.MAX_VALUE; private boolean registerMBean = REGISTER_MBEAN; private String mBeanName; /** * Construct a new instance. */ public Builder() {} /** * Get the configured thread factory. * * @return the configured thread factory (not {@code null}) */ public ThreadFactory getThreadFactory() {
/** * The builder class for an {@code EnhancedQueueExecutor}. All the fields are initialized to sensible defaults for * a small thread pool. */ public static final class Builder { private ThreadFactory threadFactory = Executors.defaultThreadFactory(); private Runnable terminationTask = NullRunnable.getInstance(); private Executor handoffExecutor = DEFAULT_HANDLER; private Thread.UncaughtExceptionHandler exceptionHandler = JBossExecutors.loggingExceptionHandler(); private int coreSize = 16; private int maxSize = 64; private long keepAliveTime = 30; private TimeUnit keepAliveUnits = TimeUnit.SECONDS; private float growthResistance; private boolean allowCoreTimeOut; private int maxQueueSize = Integer.MAX_VALUE; private boolean registerMBean = REGISTER_MBEAN; private String mBeanName; /** * Construct a new instance. */ public Builder() {} /** * Get the configured thread factory. * * @return the configured thread factory (not {@code null}) */ public ThreadFactory getThreadFactory() {
doRunTask(((TaskNode) node).getAndClearTask()); continue; } else if (node instanceof PoolThreadNode) {
final int result; for (;;) { tailNext = tail.getNext(); if (tailNext instanceof TaskNode) { TaskNode tailNextTaskNode; tailNext = tail.getNext(); } while (tailNext instanceof TaskNode); if (tail.compareAndSetNext(tailNext, tailNextNext)) { PoolThreadNode consumerNode = (PoolThreadNode) tailNext; TaskNode node = new TaskNode(runnable); if (tail.compareAndSetNext(null, node)) {
doRunTask(((TaskNode) node).getAndClearTask()); continue; } else if (node instanceof PoolThreadNode) {
final int result; for (;;) { tailNext = tail.getNext(); if (tailNext instanceof TaskNode) { TaskNode tailNextTaskNode; tailNext = tail.getNext(); } while (tailNext instanceof TaskNode); if (tail.compareAndSetNext(tailNext, tailNextNext)) { PoolThreadNode consumerNode = (PoolThreadNode) tailNext; TaskNode node = new TaskNode(runnable); if (tail.compareAndSetNext(null, node)) {
final long keepAliveTime = builder.getKeepAliveTime(TimeUnit.NANOSECONDS); head = tail = new TaskNode(null);
final long keepAliveTime = builder.getKeepAliveTime(TimeUnit.NANOSECONDS); head = tail = new TaskNode(null);
/** * Attempt to stop the thread pool immediately by interrupting all running threads and de-queueing all pending * tasks. The thread pool might not be fully stopped when this method returns, if a currently running task * does not respect interruption. * * @return a list of pending tasks (not {@code null}) */ public List<Runnable> shutdownNow() { shutdown(true); final ArrayList<Runnable> list = new ArrayList<>(); TaskNode head = this.head; QNode headNext; for (;;) { headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); head = taskNode; list.add(taskNode.task); } // retry } else { // no more tasks; return list; } } }
private QNode getOrAddNode() { TaskNode head; QNode headNext; for (;;) { head = EnhancedQueueExecutor.this.head; headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); return taskNode; } } else if (headNext instanceof PoolThreadNode || headNext == null) { PoolThreadNode newNode; newNode = new PoolThreadNode((PoolThreadNode) headNext, Thread.currentThread()); if (head.compareAndSetNext(headNext, newNode)) { return newNode; } } else { assert headNext instanceof TerminateWaiterNode; return headNext; } if (UPDATE_STATISTICS) spinMisses.increment(); } }
/** * Attempt to stop the thread pool immediately by interrupting all running threads and de-queueing all pending * tasks. The thread pool might not be fully stopped when this method returns, if a currently running task * does not respect interruption. * * @return a list of pending tasks (not {@code null}) */ public List<Runnable> shutdownNow() { shutdown(true); final ArrayList<Runnable> list = new ArrayList<>(); TaskNode head = this.head; QNode headNext; for (;;) { headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); head = taskNode; list.add(taskNode.task); } // retry } else { // no more tasks; return list; } } }
void completeTermination() { assert ! holdsLock(headLock) && ! holdsLock(tailLock); // be kind and un-interrupt the thread for the termination task Thread.interrupted(); final Runnable terminationTask = this.terminationTask; this.terminationTask = null; safeRun(terminationTask); // notify all waiters Waiter waiters = getAndSetTerminationWaiters(TERMINATE_COMPLETE_WAITER); while (waiters != null) { unpark(waiters.getThread()); waiters = waiters.getNext(); } tail.getAndSetNext(TERMINATE_COMPLETE); final ObjectInstance handle = this.handle; if (handle != null) { doPrivileged(new PrivilegedAction<Void>() { public Void run() { try { ManagementFactory.getPlatformMBeanServer().unregisterMBean(handle.getObjectName()); } catch (Throwable ignored) { } return null; } }, acc); } }
private QNode getOrAddNode() { TaskNode head; QNode headNext; for (;;) { head = EnhancedQueueExecutor.this.head; headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); return taskNode; } } else if (headNext instanceof PoolThreadNode || headNext == null) { PoolThreadNode newNode; newNode = new PoolThreadNode((PoolThreadNode) headNext, Thread.currentThread()); if (head.compareAndSetNext(headNext, newNode)) { return newNode; } } else { assert headNext instanceof TerminateWaiterNode; return headNext; } if (UPDATE_STATISTICS) spinMisses.increment(); } }
/** * The builder class for an {@code EnhancedQueueExecutor}. All the fields are initialized to sensible defaults for * a small thread pool. */ public static final class Builder { private ThreadFactory threadFactory = Executors.defaultThreadFactory(); private Runnable terminationTask = NullRunnable.getInstance(); private Executor handoffExecutor = DEFAULT_HANDLER; private Thread.UncaughtExceptionHandler exceptionHandler = JBossExecutors.loggingExceptionHandler(); private int coreSize = 16; private int maxSize = 64; private long keepAliveTime = 30; private TimeUnit keepAliveUnits = TimeUnit.SECONDS; private float growthResistance; private boolean allowCoreTimeOut; private int maxQueueSize = Integer.MAX_VALUE; private boolean registerMBean = REGISTER_MBEAN; private String mBeanName; /** * Construct a new instance. */ public Builder() {} /** * Get the configured thread factory. * * @return the configured thread factory (not {@code null}) */ public ThreadFactory getThreadFactory() {
final long keepAliveTime = builder.getKeepAliveTime(TimeUnit.NANOSECONDS); head = tail = new TaskNode(null);
/** * Attempt to stop the thread pool immediately by interrupting all running threads and de-queueing all pending * tasks. The thread pool might not be fully stopped when this method returns, if a currently running task * does not respect interruption. * * @return a list of pending tasks (not {@code null}) */ public List<Runnable> shutdownNow() { shutdown(true); final ArrayList<Runnable> list = new ArrayList<>(); TaskNode head = this.head; QNode headNext; for (;;) { headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); head = taskNode; list.add(taskNode.task); } // retry } else { // no more tasks; return list; } } }
private QNode getOrAddNode() { TaskNode head; QNode headNext; for (;;) { head = EnhancedQueueExecutor.this.head; headNext = head.getNext(); if (headNext instanceof TaskNode) { TaskNode taskNode = (TaskNode) headNext; if (compareAndSetHead(head, taskNode)) { if (! NO_QUEUE_LIMIT) decreaseQueueSize(); return taskNode; } } else if (headNext instanceof PoolThreadNode || headNext == null) { PoolThreadNode newNode; newNode = new PoolThreadNode((PoolThreadNode) headNext, Thread.currentThread()); if (head.compareAndSetNext(headNext, newNode)) { return newNode; } } else { assert headNext instanceof TerminateWaiterNode; return headNext; } if (UPDATE_STATISTICS) spinMisses.increment(); } }