-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
Do not use a separate thread for scheduling deferred tasks if possible. #817
Copy link
Copy link
Closed
Description
For simplicity, current MultithreadEventLoopGroup implementation starts a dedicated separate scheduler thread to schedule the tasks submitted via schedule*() methods. However, this is often unnecessary because SingleThreadEventLoop can adjust its wait time dynamically.
Before:
Scheduler thread:
- Keeps watching the scheduled task queue and calls SingleThreadEventLoop.execute() on time.
After
SingleThreadEventLoop:
- Determine how long should the thread wait at maximum to run the scheduled task with
closest dead line.
- poll() on a task queue or select() on a selector with the timeout calculated above.
- If waken up early due to other events like selector.wakeup(), we can always re-calculate
the new timeout.
The advantage of this change is that it reduces the latency between the scheduler and the thread that executes the scheduled task. (from non-zero to zero) .. and it's more elegant.
Reactions are currently unavailable