Netty Thread life cycle questions
Trustin Lee
tlee at redhat.com
Fri Apr 3 02:28:25 EDT 2009
Hi Twisti,
On Wed, Apr 1, 2009 at 8:13 PM, [twisti] <thetwister at gmx.de> wrote:
> Hello,
Great to see you in the forum. The forum is definitely better place
to get an answer from everyone.
> I'm having trouble understanding the life cycles of threads in Netty. Trying it myself is hard due to the way thread pools abstract that kind of thing away.
>
> Here is how I believe they work:
>
> A connection comes in. A new thread is spawned (or reused from a thread pool) to handle it's connect, despawns, and whenever a new message is received a new thread is spawned or reused. The important part here is that waiting connections that do not send or receive data do NOT occupy a thread - is that correct ? In my application, connections will typically wait around for several seconds, up to a minute, so obviously it would kill my performance if they all had threads idling around while waiting (no, sleeping threads are not free).
>
> If that is not how it works in Netty, is there a way to make it work like that ?
>
> What I want:
>
> Client connects. A thread is spawned. The thread reads in the clients request. Then, a Java timer is told to send the client a reply in, say, 20 seconds. The thread despawns, or is freed or whatever, but without the connection dropping. Then, 20 seconds later, the timer thread wakes up, and spawns a new thread which will then send the reply, close the connection and despawns again.
>
> Is this feasible ? Any ideas on how to accomplish that ?
Netty's NIO transport maintains a fixed number of I/O worker threads
and they handle all connections. Therefore, however many open
connections you have, the number of thread will remain as small as
possible.
The bottom line is that it doesn't work exactly as you described, but
it is already efficient enough not to create unnecessarily many
threads.
HTH,
— Trustin Lee, http://gleamynode.net/
More information about the netty-users
mailing list