1

java | 切换线程

 1 year ago
source link: https://benpaodewoniu.github.io/2023/01/24/java191/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

java | 切换线程 | 犀牛的博客

姑苏城外一茅屋,万树梅花月满天

eventloop 是如何切换线程的。

netty 的源码,如下

static void invokeChannelRead(final AbstractChannelHandlerContext next, Object msg) {
final Object m = next.pipeline.touch(ObjectUtil.checkNotNull(msg, "msg"), next);
EventExecutor executor = next.executor();
if (executor.inEventLoop()) { // 判断当前 handler 的线程,是否和 eventloop 在同一个线程
next.invokeChannelRead(m);
} else {
executor.execute(new Runnable() {
public void run() {
next.invokeChannelRead(m);
}
});
}

}

如果两个 handler 绑定的是同一个线程,直接调用,否则,将调用代码包装成一个任务对象,由下一个 handler 的线程调度。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK