@Override public void addKeepAliveTimeout(SelectableChannel channel, Timeout timeout) { logger.debug("added keep-alive timeout: {}", timeout); DecoratedTimeout oldTimeout = index.get(channel); if (oldTimeout != null) { keepAliveTimeouts.remove(oldTimeout); } DecoratedTimeout decorated = new DecoratedTimeout(channel, timeout); keepAliveTimeouts.add(decorated); index.put(channel, decorated); }
private long executeKeepAliveTimeouts() { // makes a defensive copy to avoid (1) CME (new timeouts are added this iteration) and (2) IO // starvation. TreeMultiset<DecoratedTimeout> defensive = TreeMultiset.create(keepAliveTimeouts); Iterator<DecoratedTimeout> iter = defensive.iterator(); final long now = System.currentTimeMillis(); while (iter.hasNext()) { DecoratedTimeout candidate = iter.next(); if (candidate.timeout.getTimeout() > now) { break; } candidate.timeout.getCallback().onCallback(); index.remove(candidate.channel); iter.remove(); keepAliveTimeouts.remove(candidate); logger.debug("Keep-alive timeout triggered: {}", candidate.timeout); } return keepAliveTimeouts.isEmpty() ? Long.MAX_VALUE : Math.max(1, keepAliveTimeouts.iterator().next().timeout.getTimeout() - now); }
public String handleCase2(InputData in) { /** * The idea is we put the levels in 2 sets. * * <p>One is ordered by least set 2 star prereq first ; breaking ties by higher 1 star. * * <p>TODO why 1 star sort? */ Ordering<Level> set2Ordering = Ordering.from( new Comparator<Level>() { @Override public int compare(Level o1, Level o2) { return ComparisonChain.start() .compare(o1.twoStarPrereq, o2.twoStarPrereq) .compare(o2.oneStarPrereq, o1.oneStarPrereq) .result(); } }); TreeMultiset<Level> set2star = TreeMultiset.create(set2Ordering); /** * If we need to play a 1 star, play the one with the highest 2 star req. This is because we may * be able to play a lower 2 star req in 1 round later on. */ TreeMultiset<Level> set1star = TreeMultiset.create( new Comparator<Level>() { @Override public int compare(Level o1, Level o2) { return ComparisonChain.start() .compare(o1.oneStarPrereq, o2.oneStarPrereq) .compare(o2.twoStarPrereq, o1.twoStarPrereq) .result(); } }); set1star.addAll(in.levels); set2star.addAll(in.levels); int score = 0; int count = 0; while (!set2star.isEmpty()) { Level nextSet2 = set2star.firstEntry().getElement(); if (nextSet2.twoStarPrereq <= score) { ++count; if (set1star.contains(nextSet2)) { score++; set1star.remove(nextSet2); } set2star.remove(nextSet2); score++; continue; } List<Level> set1Choices = new ArrayList<>(set1star.headMultiset(new Level(score, 0), BoundType.CLOSED)); Collections.sort(set1Choices, set2Ordering); if (nextSet2.twoStarPrereq > score) { // Need to take from set1 with the highest possible round 2 score if (set1Choices.isEmpty()) { return String.format("Case #%d: Too Bad", in.testCase); } ++count; score += 1; Level round1 = set1Choices.remove(set1Choices.size() - 1); set1star.remove(round1); continue; } } // in.levelMin.subList() return String.format("Case #%d: %s", in.testCase, count); }