Improved HashedWheelTimer.stop() to return a set of unprocessed timeouts

This commit is contained in:
Trustin Lee 2009-01-20 12:14:29 +00:00
parent 5249c47da7
commit 6894b0f277
2 changed files with 15 additions and 4 deletions

View File

@ -23,6 +23,8 @@
package org.jboss.netty.handler.timeout;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ThreadFactory;
@ -144,10 +146,11 @@ public class HashedWheelTimer implements Timer {
workerThread.start();
}
public void stop() {
public Set<Timeout> stop() {
if (!shutdown.compareAndSet(false, true)) {
return;
return Collections.emptySet();
}
while (workerThread.isAlive()) {
workerThread.interrupt();
try {
@ -156,6 +159,14 @@ public class HashedWheelTimer implements Timer {
// Ignore
}
}
Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
for (Set<HashedWheelTimeout> bucket: wheel) {
unprocessedTimeouts.addAll(bucket);
bucket.clear();
}
return Collections.unmodifiableSet(unprocessedTimeouts);
}
public Timeout newTimeout(TimerTask task, long initialDelay, TimeUnit unit) {

View File

@ -22,6 +22,7 @@
*/
package org.jboss.netty.handler.timeout;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
@ -31,6 +32,5 @@ import java.util.concurrent.TimeUnit;
*/
public interface Timer {
Timeout newTimeout(TimerTask task, long timeout, TimeUnit unit);
// XXX Should we make stop() return the list of unfinished Timeouts?
void stop();
Set<Timeout> stop();
}