我想知道 Java NIO 中的标准/最佳实现是什么。这是实现每 N 秒一次心跳等功能的基础。 注意:由于明显的原因(线程是邪恶的并且上下文切换很慢),一切都必须始终发生在选择器循环内。
注1:回答 Apache MINA 不算数,除非您和框架能够演示一个清晰的场景,其中该场景是在 KISS(保持简单愚蠢)方式。
注2:管道需要螺纹。
I was wondering what is the standard/best implementation of that in Java NIO. This is fundamental to implement something like heartbeats every N seconds, etc. Note: For obvious reasons (threads are evil and context switches are slow) everything must always happens inside the selector loop.
Note1: Answering Apache MINA does not count, unless you and the framework can demonstrate a clear scenario where this is done in a KISS (Keep It Simple Stupid) way.
Note2: Pipes require threads.
发布评论
评论(2)
不确定为什么没有后台线程来发送心跳或超时连接。心跳通常不被认为对性能至关重要。
您可以让选择器等待特定的时间并发送心跳并定期检查超时。
你的意思是像
Not sure why you wouldn't have a background thread for sending heartbeats or timing out connections. Heartbeats are not generally considered performance critical.
You can have the selector wait a specific amount of time and send heartbeats and check time outs at intervals.
Do you mean like
创建一个管道,在选择器中注册可读管道的末端,调度一个计时器,在计时器回调中向可写管道的末端写入一个字节。 IO 处理程序应将管道可读事件视为心跳触发器。
create a pipe, register readable pipe's end in selector, schedule a timer, in the timer callback write one byte into the writable pipe's end. The IO handler should treat pipe readable event as a heartbeat trigger.