Minimized the number of rewind() calls
This commit is contained in:
parent
e4f23c16d6
commit
3e0d1f69b0
@ -23,10 +23,11 @@
|
|||||||
package org.jboss.netty.handler.timeout;
|
package org.jboss.netty.handler.timeout;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.concurrent.Executor;
|
import java.util.concurrent.Executor;
|
||||||
|
import java.util.concurrent.Executors;
|
||||||
|
import java.util.concurrent.ThreadPoolExecutor;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
import java.util.concurrent.atomic.AtomicInteger;
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
import java.util.concurrent.locks.ReadWriteLock;
|
import java.util.concurrent.locks.ReadWriteLock;
|
||||||
@ -251,7 +252,6 @@ public class HashedWheelTimer implements Timer {
|
|||||||
wheelCursor = newBucketHead;
|
wheelCursor = newBucketHead;
|
||||||
|
|
||||||
ReusableIterator<HashedWheelTimeout> i = iterators[oldBucketHead];
|
ReusableIterator<HashedWheelTimeout> i = iterators[oldBucketHead];
|
||||||
i.rewind();
|
|
||||||
fetchExpiredTimeouts(expiredTimeouts, i);
|
fetchExpiredTimeouts(expiredTimeouts, i);
|
||||||
|
|
||||||
if (activeTimeouts.get() == 0) {
|
if (activeTimeouts.get() == 0) {
|
||||||
@ -274,26 +274,30 @@ public class HashedWheelTimer implements Timer {
|
|||||||
|
|
||||||
private void fetchExpiredTimeouts(
|
private void fetchExpiredTimeouts(
|
||||||
List<HashedWheelTimeout> expiredTimeouts,
|
List<HashedWheelTimeout> expiredTimeouts,
|
||||||
Iterator<HashedWheelTimeout> i) {
|
ReusableIterator<HashedWheelTimeout> i) {
|
||||||
|
|
||||||
long currentTime = System.nanoTime();
|
long currentTime = System.nanoTime();
|
||||||
while (i.hasNext()) {
|
boolean notEmpty = i.hasNext();
|
||||||
HashedWheelTimeout timeout = i.next();
|
if (notEmpty) {
|
||||||
synchronized (timeout) {
|
do {
|
||||||
if (timeout.remainingRounds <= 0) {
|
HashedWheelTimeout timeout = i.next();
|
||||||
if (timeout.deadline <= currentTime) {
|
synchronized (timeout) {
|
||||||
i.remove();
|
if (timeout.remainingRounds <= 0) {
|
||||||
expiredTimeouts.add(timeout);
|
if (timeout.deadline <= currentTime) {
|
||||||
activeTimeouts.getAndDecrement();
|
i.remove();
|
||||||
|
expiredTimeouts.add(timeout);
|
||||||
|
activeTimeouts.getAndDecrement();
|
||||||
|
} else {
|
||||||
|
// A rare case where a timeout is put for the next
|
||||||
|
// round: just wait for the next round.
|
||||||
|
timeout.slippedRounds ++;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
// A rare case where a timeout is put for the next
|
timeout.remainingRounds --;
|
||||||
// round: just wait for the next round.
|
|
||||||
timeout.slippedRounds ++;
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
timeout.remainingRounds --;
|
|
||||||
}
|
}
|
||||||
}
|
} while (i.hasNext());
|
||||||
|
i.rewind();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -499,4 +503,25 @@ public class HashedWheelTimer implements Timer {
|
|||||||
return buf.append(')').toString();
|
return buf.append(')').toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Exception {
|
||||||
|
final ThreadPoolExecutor e = (ThreadPoolExecutor) Executors.newCachedThreadPool();
|
||||||
|
Timer timer = new HashedWheelTimer(
|
||||||
|
e,
|
||||||
|
100, TimeUnit.MILLISECONDS, 4);
|
||||||
|
|
||||||
|
//Timeout timeout = timer.newTimeout(1200, TimeUnit.MILLISECONDS);
|
||||||
|
for (int i = 0; i < 1; i ++) {
|
||||||
|
timer.newTimeout(new TimerTask() {
|
||||||
|
public void run(Timeout timeout) throws Exception {
|
||||||
|
//System.out.println(Thread.currentThread().getName() + ": " + timeout.getExtensionCount() + ": " + timeout);
|
||||||
|
timeout.extend();
|
||||||
|
int c = e.getActiveCount();
|
||||||
|
if (c > 1) {
|
||||||
|
System.out.println(System.currentTimeMillis() + ": " + c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, 100, TimeUnit.MILLISECONDS);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user