简体   繁体   中英

Why Netty need thread pools?

Coming from a node.js background, I'm confused that it's not single threaded in Netty as in node.js. (The NioServerSocketChannelFactory document states that there are boss threads and worker threads)

Maybe it's because unlike in node.js, many of the existing Java libs are not async. But even if it's the case, why not just let users to create threads as necessary? Isn't it more natural and conceptually aligned with async event-driven architecture?

My knowledge of node.js and javascript are relatively limited, but isn't node.js limited by the fact that javascript has no support for multithreading?

The use of boss and worker threads in Netty is about being able to utilise multiple processor cores without needing to run multiple processes. The JVM is quite heavy so it makes sense to limit the number of instances needed. Also I suspect it's far quicker for an operating system to switch between threads than complete processes.

Boss threads are used to accept incoming connections but the processing of a connection is handed off to a worker thread. I don't have a reference to hand but I dimly remember the Grizzly project publishing some performance results showing this was more performant than trying to accept incoming connections, and process those connections, in the same thread.

Accepted connections are load balanced on worker threads in a round robin fashion. The worker threads do not interact with each other unless you explicitly write code to do so. Therefore the worker thread model is pretty much identical to my understanding of node.js.

Netty deals with non-async libraries by providing mechanisms which allow an application to define a thread model, via thread pools, which is independent of the boss and worker threads. To help with this Netty also provides a few custom thread pools which are aware of the memory resources being used by a given connection, and can also guarantee the ordering of events generated by connection. However, it is not necessary to use these mechanisms. An application is free to use whatever thread model best suits.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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