void () { final long startTime = System.currentTimeMillis() - myUpdateInterval.getAsLong(); boolean onlyAbsolute = true; // TODO: Actually we could store items with pair.First == null in separate list. // checks item that has smallest update time - i.e. was not updated by the most time // if its update time greater than current - interval => we should not update any item with pair.First != null this time (as they are ordered) synchronized (myLock) { for (Pair<Long, T> pair : myQueue) { if (pair.getFirst() != null) { onlyAbsolute = pair.getFirst() > startTime; break; } } } // do not ask under lock final Boolean shouldUpdateOld = onlyAbsolute ? false : myShouldUpdateOldChecker.compute(); final List<T> dirty = new ArrayList<>(); synchronized (myLock) { // adds all pairs with pair.First == null to dirty while (! myQueue.isEmpty()) { final Pair<Long, T> pair = myQueue.get(0); if (pair.getFirst() == null) { dirty.add(myQueue.removeFirst().getSecond()); } else { break; } } if (Boolean.TRUE.equals(shouldUpdateOld) && (! myQueue.isEmpty())) { // adds all pairs with update time (pair.First) < current - interval to dirty while (! myQueue.isEmpty()) { final Pair<Long, T> pair = myQueue.get(0); if (pair.getFirst() < startTime) { myQueue.removeFirst(); dirty.add(pair.getSecond()); } else { break; } } } myInProgress.addAll(dirty); } LOG.debug("found something to update: " + (! dirty.isEmpty())); for (T t : dirty) { myUpdater.consume(t); synchronized (myLock) { // output value of remove() is tracked not to process items that were removed from myInProgress in forceRemove() // TODO: Probably more clear logic should be implemented if (myInProgress.remove(t)) { myQueue.addLast(new Pair<>(System.currentTimeMillis(), t)); } } } }