Runnable (long status) { while (true) { int inProgress = getTasksInProgress(status); assert inProgress > 0 : inProgress; Runnable next; if (inProgress <= myMaxThreads && (next = myTaskQueue.poll()) != null) { return next; } if (myStatus.compareAndSet(status, status - 1)) { break; } status = myStatus.get(); } return null; }