Basic Boss/Worker Thread Question

ljohnston johnstlr at yahoo.co.uk
Mon Nov 1 14:25:39 EDT 2010



balluk wrote:
> 
> Please let me know if my understanding of Boss/Worker thread concept in
> Netty is correct:
> A Boss thread is created whenever a server is bound and a worker thread is
> created whenever a client connects to the bound server.  So for every
> client/server connection a worker thread is created.
> 

This is not true when working with NIO channels. With NIO a separate Boss
thread is used to process each bound server socket channel. However when a
client connects the channel is assigned to a worker thread taken from a
pool. A worker thread can be handling many channels which is what helps NIO
scale to thousands (tens of thousands) of connections. This is is also why
it's important to not perform any blocking operations in the worker thread
as doing so will block all the other channels it is handling.


balluk wrote:
> 
> Here is my requirement:  A single client connects to a single server.  The
> client sends multiple messages to the server, each message should be
> handled asynchronously by a different thread ie each thread handles one
> message.
> 

You need to add an executor thread pool to your pipeline. This is achieved
by wrapping the executor in an 
http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/execution/ExecutionHandler.html
ExecutionHandler .

You can use any executor you like but, based on your description, I suggest
you look at 
http://docs.jboss.org/netty/3.2/api/org/jboss/netty/handler/execution/MemoryAwareThreadPoolExecutor.html
MemoryAwareThreadPoolExecutor  which will automatically throttle channels if
the client is sending messages faster than you can process them.

Note you can either create a new executor per client connection or you can
share the executor across all clients.

Cheers
Lee

-- 
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/Basic-Boss-Worker-Thread-Question-tp5691357p5694637.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list