Worker Class — netty Architecture
Architecture documentation for the Worker class in HashedWheelTimer.java from the netty codebase.
Entity Profile
Dependency Diagram
graph TD 97b31b84_3527_3690_913b_44ff6fafb9cc["Worker"] 8be6e914_922c_a17c_9e45_cdcc72002795["HashedWheelTimer.java"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|defined in| 8be6e914_922c_a17c_9e45_cdcc72002795 ea613877_8294_fd85_27b0_0c4903842ef1["run()"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|method| ea613877_8294_fd85_27b0_0c4903842ef1 1a7f3952_1dcf_b390_f3fc_948e608ef456["transferTimeoutsToBuckets()"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|method| 1a7f3952_1dcf_b390_f3fc_948e608ef456 f4e4374f_cea1_44a1_d8cc_b9bc9e38ac91["processCancelledTasks()"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|method| f4e4374f_cea1_44a1_d8cc_b9bc9e38ac91 9f207468_c502_a117_0557_1b7c3aca33b8["waitForNextTick()"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|method| 9f207468_c502_a117_0557_1b7c3aca33b8 931ac4e6_38e5_402d_fc9f_04aca2ea7f22["unprocessedTimeouts()"] 97b31b84_3527_3690_913b_44ff6fafb9cc -->|method| 931ac4e6_38e5_402d_fc9f_04aca2ea7f22
Relationship Graph
Source Code
common/src/main/java/io/netty/util/HashedWheelTimer.java lines 476–610
private final class Worker implements Runnable {
private final Set<Timeout> unprocessedTimeouts = new HashSet<Timeout>();
private long tick;
@Override
public void run() {
// Initialize the startTime.
startTime = System.nanoTime();
if (startTime == 0) {
// We use 0 as an indicator for the uninitialized value here, so make sure it's not 0 when initialized.
startTime = 1;
}
// Notify the other threads waiting for the initialization at start().
startTimeInitialized.countDown();
do {
final long deadline = waitForNextTick();
if (deadline > 0) {
int idx = (int) (tick & mask);
processCancelledTasks();
HashedWheelBucket bucket =
wheel[idx];
transferTimeoutsToBuckets();
bucket.expireTimeouts(deadline);
tick++;
}
} while (WORKER_STATE_UPDATER.get(HashedWheelTimer.this) == WORKER_STATE_STARTED);
// Fill the unprocessedTimeouts so we can return them from stop() method.
for (HashedWheelBucket bucket: wheel) {
bucket.clearTimeouts(unprocessedTimeouts);
}
for (;;) {
HashedWheelTimeout timeout = timeouts.poll();
if (timeout == null) {
break;
}
if (!timeout.isCancelled()) {
unprocessedTimeouts.add(timeout);
}
}
processCancelledTasks();
}
private void transferTimeoutsToBuckets() {
// transfer only max. 100000 timeouts per tick to prevent a thread to stale the workerThread when it just
// adds new timeouts in a loop.
for (int i = 0; i < 100000; i++) {
HashedWheelTimeout timeout = timeouts.poll();
if (timeout == null) {
// all processed
break;
}
if (timeout.state() == HashedWheelTimeout.ST_CANCELLED) {
// Was cancelled in the meantime.
continue;
}
long calculated = timeout.deadline / tickDuration;
timeout.remainingRounds = (calculated - tick) / wheel.length;
final long ticks = Math.max(calculated, tick); // Ensure we don't schedule for past.
int stopIndex = (int) (ticks & mask);
HashedWheelBucket bucket = wheel[stopIndex];
bucket.addTimeout(timeout);
}
}
private void processCancelledTasks() {
for (;;) {
HashedWheelTimeout timeout = cancelledTimeouts.poll();
if (timeout == null) {
// all processed
break;
}
try {
timeout.removeAfterCancellation();
} catch (Throwable t) {
Source
Frequently Asked Questions
What is the Worker class?
Worker is a class in the netty codebase, defined in common/src/main/java/io/netty/util/HashedWheelTimer.java.
Where is Worker defined?
Worker is defined in common/src/main/java/io/netty/util/HashedWheelTimer.java at line 476.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free