@Override public void run() { synchronized (lock) { // make sure we only have 5 ping in parallel if (shutdown || COUNTER.get() > MAX_PING) { return; } for (Maintainable maintainable : maintainables) { PeerStatistic peerStatatistic = maintainable.nextForMaintenance(runningFutures.values()); if (peerStatatistic == null) { continue; } BaseFuture future; if (peerStatatistic.isLocal()) { future = peer.localAnnounce().ping().peerAddress(peerStatatistic.peerAddress()).start(); LOG.debug( "maintenance local ping from {} to {}", peer.peerAddress(), peerStatatistic.peerAddress()); } else { future = peer.ping().peerAddress(peerStatatistic.peerAddress()).start(); LOG.debug( "maintenance ping from {} to {}", peer.peerAddress(), peerStatatistic.peerAddress()); } peer.notifyAutomaticFutures(future); runningFutures.put(future, peerStatatistic.peerAddress()); COUNTER.incrementAndGet(); future.addListener( new BaseFutureAdapter<BaseFuture>() { @Override public void operationComplete(BaseFuture future) throws Exception { synchronized (lock) { runningFutures.remove(future); COUNTER.decrementAndGet(); } } }); } } }
public FutureDone<Void> shutdown() { if (scheduledFuture != null) { scheduledFuture.cancel(false); // TODO: check if synchronized is really necessary here } final FutureDone<Void> futureShutdown = new FutureDone<Void>(); synchronized (lock) { shutdown = true; final int max = runningFutures.size(); final AtomicInteger counter = new AtomicInteger(0); for (BaseFuture future : runningFutures.keySet()) { future.addListener( new BaseFutureAdapter<BaseFuture>() { @Override public void operationComplete(BaseFuture future) throws Exception { if (counter.incrementAndGet() == max) { futureShutdown.done(); } } }); } } return futureShutdown; }