public void oneExecutionDone(Runnable triggerCheckForNewBatch) {
    // May be called concurrently by multiple threads
    executionsLeftInBatch.decrementAndGet();

    LOG.trace(
        "Batch state: generationNumber:{}, stale:{}, triggeredExecuteDue:{}, possiblyMoreExecutionsInDb:{}, executionsLeftInBatch:{}",
        generationNumber,
        stale,
        triggeredExecuteDue,
        possiblyMoreExecutionsInDb,
        executionsLeftInBatch.get());
    // Will not synchronize this method as it is not a big problem if two threads manage to call
    // triggerCheckForNewBatch.run() at the same time.
    // There is synchronization further in, when waking the thread that will do the fetching.
    if (!stale
        && !triggeredExecuteDue
        && possiblyMoreExecutionsInDb
        && whenToTriggerCheckForNewBatch.test(executionsLeftInBatch.get())) {
      LOG.trace("Triggering check for new batch.");
      triggerCheckForNewBatch.run();
      triggeredExecuteDue = true;
    }
  }
@param triggerCheckForNewBatch may be triggered more than one in racy conditions
db-scheduler/src/main/java/com/github/kagkarlsson/scheduler/DueExecutionsBatch.java