.NET相当于Java的NIO选择器 [英] .NET equvalent of Java's NIO Selector

查看:39
本文介绍了.NET相当于Java的NIO选择器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道.NET对于网络I/O具有良好的异步模型,该模型在幕后使用了完成端口.但是所有回调都发生在线程池中的IO线程上.

I know .NET has a good asynchronous model for network I/O, which uses completion port under the hood. But all the callbacks are happening on IO threads from thread pool.

是否有与Java选择器类似的.NET对应项,可在单个线程中处理多个流?或者,线程池回调比这种单线程方法具有更好的伸缩性?

Is there any .NET counterparts like Java's selector which deals with multiple streams in a single thread? Or, the thread pool callbacks scale better than this single threaded approach?

谢谢

多德

推荐答案

对于异步操作,IO处理与线程池关联.当异步操作完成时,我相信(针对每个流的)回调可能会或可能不会使用同一线程执行,任何可用的线程池线程都可以处理该回调,同一线程很可能会处理多个回调或仅一个根据运行时条件进行回调.

For async operations an IO handle is associated with the thread pool. When the async operation completes, I believe that the callbacks(for each stream) may or may not execute using the same thread, any available thread pool thread could process the callback, it's quite possible that the same thread could process multiple callbacks or just one callback based on runtime conditions.

希望这会有所帮助

添加对多德评论的回复

我不是很熟悉Selector API,而是通过查看发布的示例

I'm not intimately familiar with the Selector API but from looking at an example posted here it seems that Selector waits until all events occur. Is that true? If so then the caller would have to wait for all events to occur even when one event occurs sooner than the another. But if the Selector works by processing an event as soon as it occurs, one could run into a situation where the selector is processing the callback for one event while another event arrives(I would imagine that in this case the incoming event would get queued somewhere or else you would be dropping events) but it would still reduce the throughput when the events are orthogonal and should be processed as soon as they occur.

.NET中的异步模型以线程池为中心,以减少创建新线程的开销(因为这是一项昂贵的操作).如果观察到线程池已用完,则可以将池中的线程数增加为

The async model in .NET is centered around the thread pool to reduce the overhead of creating a new thread(since it's an expensive operation). If you are observing that the thread pool is maxing out you could increase the number of Thread in the pool as documented here. Bear in mind though that at the end of the day you are limited to the number of processors i.e On a dual core box only 2 threads can be actively running, all others are blocked, so that might be something to take into account.

希望这会有所帮助.

这篇关于.NET相当于Java的NIO选择器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆