/** Handles size after put. */ private void onPut() { cnt.incrementAndGet(); int c; while ((c = cnt.get()) > max) { // Decrement count. if (cnt.compareAndSet(c, c - 1)) { try { K key = firstEntry().getKey(); V val; // Make sure that an element is removed. while ((val = super.remove(firstEntry().getKey())) == null) { // No-op. } assert val != null; GridInClosure2<K, V> lsnr = this.lsnr; // Listener notification. if (lsnr != null) lsnr.apply(key, val); } catch (NoSuchElementException e1) { e1.printStackTrace(); // Should never happen. assert false : "Internal error in grid bounded ordered set."; } } } }