Scans for and, if found, returns one task, else possibly
inactivates the worker. This method operates on single reads of
volatile state and is designed to be re-invoked continuously,
in part because it returns upon detecting inconsistencies,
contention, or state changes that indicate possible success on
The scan searches for tasks across a random permutation of
queues (starting at a random index and stepping by a random
relative prime, checking each at least once). The scan
terminates upon either finding a non-empty queue, or completing
the sweep. If the worker is not inactivated, it takes and
returns a task from this queue. On failure to find a task, we
take one of the following actions, after which the caller will
retry calling this method unless terminated.
* If pool is terminating, terminate the worker.
* If not a complete sweep, try to release a waiting worker. If
the scan terminated because the worker is inactivated, then the
released worker will often be the calling worker, and it can
succeed obtaining a task on the next call. Or maybe it is
another worker, but with same net effect. Releasing in other
cases as well ensures that we have enough workers running.
* If not already enqueued, try to inactivate and enqueue the
worker on wait queue. Or, if inactivating has caused the pool
to be quiescent, relay to idleAwaitWork to check for
termination and possibly shrink pool.
* If already inactive, and the caller has run a task since the
last empty scan, return (to allow rescan) unless others are
also inactivated. Field WorkQueue.rescans counts down on each
scan to ensure eventual inactivation and blocking.
* If already enqueued and none of the above apply, park