void (@NotNull Runnable command) { Runnable task = command instanceof LastTask ? command : AppScheduledExecutorService.capturePropagationAndCancellationContext(command); if (isShutdown() && !(task instanceof LastTask)) { throw new RejectedExecutionException(this+" is already shutdown, trying to execute "+task+" ("+task.getClass()+")"); } long status = incrementCounterAndTimestamp(); // increment inProgress and queue-stamp atomically int inProgress = getTasksInProgress(status); assert inProgress > 0 : inProgress; if (inProgress <= myMaxThreads) { // optimisation: can execute without queue/dequeue wrapAndExecute(task, status); return; } if (!myTaskQueue.offer(task)) { throw new RejectedExecutionException(); } Runnable next = pollOrGiveUp(status); if (next != null) { wrapAndExecute(next, status); } }