/** 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."; } } } }
/** {@inheritDoc} */ @Override public void onCollision( Collection<GridCollisionJobContext> waitJobs, Collection<GridCollisionJobContext> activeJobs) { assert waitJobs != null; assert activeJobs != null; int activeSize = F.size(activeJobs, RUNNING_JOBS); waitingCnt.set(waitJobs.size()); runningCnt.set(activeSize); heldCnt.set(activeJobs.size() - activeSize); int waitSize = waitJobs.size(); int activateCnt = parallelJobsNum - activeSize; if (activateCnt > 0 && !waitJobs.isEmpty()) { if (waitJobs.size() <= activateCnt) { for (GridCollisionJobContext waitJob : waitJobs) { waitJob.activate(); waitSize--; } } else { List<GridCollisionJobContext> passiveList = new ArrayList<GridCollisionJobContext>(waitJobs); Collections.sort( passiveList, new Comparator<GridCollisionJobContext>() { /** {@inheritDoc} */ @Override public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) { int p1 = getJobPriority(o1); int p2 = getJobPriority(o2); return p1 < p2 ? 1 : p1 == p2 ? 0 : -1; } }); if (preventStarvation) bumpPriority(waitJobs, passiveList); for (int i = 0; i < activateCnt; i++) { passiveList.get(i).activate(); waitSize--; } } } if (waitSize > waitJobsNum) { List<GridCollisionJobContext> waitList = new ArrayList<GridCollisionJobContext>(waitJobs); // Put jobs with highest priority first. Collections.sort( waitList, new Comparator<GridCollisionJobContext>() { /** {@inheritDoc} */ @Override public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) { int p1 = getJobPriority(o1); int p2 = getJobPriority(o2); return p1 < p2 ? 1 : p1 == p2 ? 0 : -1; } }); int skip = waitJobs.size() - waitSize; int i = 0; for (GridCollisionJobContext waitCtx : waitList) { if (++i >= skip) { waitCtx.cancel(); if (--waitSize <= waitJobsNum) break; } } } }
/** {@inheritDoc} */ @Override public int getCurrentHeldJobsNumber() { return heldCnt.get(); }
/** {@inheritDoc} */ @Override public int getCurrentRunningJobsNumber() { return runningCnt.get(); }
/** {@inheritDoc} */ @Override public int getCurrentActiveJobsNumber() { return runningCnt.get() + heldCnt.get(); }
/** {@inheritDoc} */ @Override public int getCurrentWaitJobsNumber() { return waitingCnt.get(); }
/** * Approximate size at this point of time. Note, that unlike {@code size} methods on other {@code * concurrent} collections, this method executes in constant time without traversal of the * elements. * * @return Approximate set size at this point of time. */ @Override public int size() { return cnt.get(); }