简体   繁体   English

使用NIO的Node.js非阻塞IO与Java线程池模式。 调度行为不清楚

[英]Node.js non-blocking IO vs. java thread-pool pattern using NIO. unclear scheduling behaviour

Please help me to understand the disadvantage of Thread-Pool pattern using NIO. 请帮助我了解使用NIO的线程池模式的缺点。

I found this Question: Java NIO non-blocking mode vs node.js asychronous operation but it do not answer how NIO affects scheduling behaviour in java. 我发现了这个问题: Java NIO非阻塞模式与node.js异步操作,但是它无法回答NIO如何影响Java中的调度行为。

How I understand Node.js and java.IO: 我如何理解Node.js和java.IO:

Node.js uses Event loop and non-blocking IO combined with callbacks. Node.js将事件循环和非阻塞IO与回调结合使用。 This allows to process other tasks while IO-call not finished like this: 这样可以在IO调用未完成时处理其他任务,如下所示:

node.js中的事件循环

Using old java.IO library and thread pool pattern the java thread get blocked till IO operation is completed. 使用旧的java.IO库和线程池模式,java线程将被阻塞,直到IO操作完成为止。 In the meantime the thread can not process other tasks and barred from scheduling. 同时,线程无法处理其他任务并被禁止调度。

But now. 但现在。 What about NIO? 那蔚来呢? Consider we have a thread pool with max 10 threads. 考虑我们有一个最多有10个线程的线程池。 Every thread get some job as Runnable object. 每个线程都作为Runnable对象获得一些工作。

So what will happen if a long-term IO operation is required. 因此,如果需要长期的IO操作,将会发生什么。 I can't believe that the current undone Runnable will be replaced by other till the initial IO call is completed. 我不敢相信在初始IO调用完成之前,当前已撤消的Runnable将被其他替换。

So how exactly changes java.NIO the scheduling behaviour in Java by comparison with java.IO ? 那么,与java.IO比较,如何精确地改变java.NIO在Java中的调度行为?

In essence, NIO provides the means to program your own single-threaded event-loop similar to node.js. 本质上,NIO提供了一种类似于node.js的编程自己的单线程事件循环的方法。 Java IO, in comparison, provided no such means. 相比之下,Java IO没有提供这种方法。

Implementations could differ, but in essence, you could create a thread which reads from Selector and executes some registered callbacks for each successful select. 实现可能有所不同,但从本质上讲,您可以创建一个线程,该线程从Selector中读取并为每个成功的select执行一些已注册的回调。 Then, for each new kind of IO, you could register it's Channel (or anything) with same Selector, and in this way implement 100% the same (but much more Flexible) implementation of single-threaded callback event loop. 然后,对于每种新的IO,您可以使用相同的Selector注册它的Channel(或其他任何东西),并以此方式实现100%相同(但更加灵活)的单线程回调事件循环实现。 By itself, NIO does not provide such event loop or anything similar, if that's what you are asking. 如果您要的是,NIO本身不提供此类事件循环或类似功能。

It's worth to notice, that the implementation behind NIO differs depending on infrastructure underneath. 值得注意的是,NIO背后的实现因底层基础架构而异。 For example writing to file using NIO in POSIX systems would translate to native OS calls, while running it one some blocking-IO-only-OS would make Java create a separate thread for each IO for backward compatibility (as there would technically be no other way to do NIO). 例如,在POSIX系统中使用NIO写入文件将转换为本机OS调用,而在运行该程序时,某些仅IO阻塞的OS将使Java为每个IO创建一个单独的线程以实现向后兼容(因为从技术上讲,没有其他线程NIO的方法)。

I hope that answers your question, which was a bit blurry. 我希望能回答您的问题,这有点模糊。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM