Worker
Class Worker mainly maintains interrupt control state for threads running tasks, along with other minor bookkeeping. This class opportunistically extends AbstractQueuedSynchronizer to simplify acquiring and releasing a lock surrounding each task execution. This protects against interrupts that are intended to wake up a worker thread waiting for a task from instead interrupting a task being run. We implement a simple non-reentrant mutual exclusion lock rather than use ReentrantLock because we do not want worker tasks to be able to reacquire the lock when they invoke pool control methods like setCorePoolSize. Additionally, to suppress interrupts until the thread actually starts running tasks, we initialize lock state to a negative value, and clear it upon start (in runWorker).
Properties
Functions
Acquires in exclusive mode, ignoring interrupts. Implemented by invoking at least once .tryAcquire, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success. This method can be used to implement method Lock.lock.
Main acquire method, invoked by all exported acquire methods.
Acquires in exclusive mode, aborting if interrupted. Implemented by first checking interrupt status, then invoking at least once .tryAcquire, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success or the thread is interrupted. This method can be used to implement method Lock.lockInterruptibly.
Acquires in shared mode, ignoring interrupts. Implemented by first invoking at least once .tryAcquireShared, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success.
Acquires in shared mode, aborting if interrupted. Implemented by first checking interrupt status, then invoking at least once .tryAcquireShared, returning on success. Otherwise the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success or the thread is interrupted.
Returns true if the apparent first queued thread, if one exists, is waiting in exclusive mode. If this method returns true, and the current thread is attempting to acquire in shared mode (that is, this method is invoked from .tryAcquireShared) then it is guaranteed that the current thread is not the first queued thread. Used only as a heuristic in ReentrantReadWriteLock.
Enqueues the node unless null. (Currently used only for ConditionNodes; other cases are interleaved with acquires.)
Returns a collection containing those threads that may be waiting on the given condition associated with this synchronizer. Because the actual set of threads may change dynamically while constructing this result, the returned collection is only a best-effort estimate. The elements of the returned collection are in no particular order.
Returns an estimate of the number of threads waiting on the given condition associated with this synchronizer. Note that because timeouts and interrupts may occur at any time, the estimate serves only as an upper bound on the actual number of waiters. This method is designed for use in monitoring system state, not for synchronization control.
Queries whether any threads have ever contended to acquire this synchronizer; that is, if an acquire method has ever blocked.
Queries whether any threads have been waiting to acquire longer than the current thread.
Queries whether any threads are waiting to acquire. Note that because cancellations due to interrupts and timeouts may occur at any time, a true return does not guarantee that any other thread will ever acquire.
Queries whether any threads are waiting on the given condition associated with this synchronizer. Note that because timeouts and interrupts may occur at any time, a true return does not guarantee that a future signal will awaken any threads. This method is designed primarily for use in monitoring of the system state.
Returns true if node is found in traversal from tail
Queries whether the given ConditionObject uses this synchronizer as its lock.
Releases in exclusive mode. Implemented by unblocking one or more threads if .tryRelease returns true. This method can be used to implement method Lock.unlock.
Releases in shared mode. Implemented by unblocking one or more threads if .tryReleaseShared returns true.
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 .tryAcquire, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquire until success or the thread is interrupted or the timeout elapses. This method can be used to implement method Lock.tryLock.
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 .tryAcquireShared, returning on success. Otherwise, the thread is queued, possibly repeatedly blocking and unblocking, invoking .tryAcquireShared until success or the thread is interrupted or the timeout elapses.