int readyCount;
// If there are any pending timeouts, block no longer than
// the earliest. Otherwise, block indefinitely.
final Delayed nextExpiringTask = timeouts.peek();
if (nextExpiringTask == null) {
readyCount = selector.select();
} else {
long nextTimeoutMillis =
nextExpiringTask.getDelay(TimeUnit.MILLISECONDS);
if (nextTimeoutMillis <= 0) {
readyCount = selector.selectNow();
} else {
readyCount = selector.select(nextTimeoutMillis);
}