Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Giovanni Azua Newsgroups: comp.lang.java.programmer Subject: Re: NIO multiplexing + thread pooling Date: Sat, 24 Sep 2011 23:09:17 +0200 Lines: 42 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Trace: individual.net fL+Fa0eQOOVYwc/1tO+UgAHgGIlLGzHBOzZmunOBK82OxVOz8B Cancel-Lock: sha1:OP2CgVo9LREzUkcBE4+47zrhxcI= User-Agent: Microsoft-Entourage/12.30.0.110427 Thread-Topic: NIO multiplexing + thread pooling Thread-Index: Acx6/jgyqsrSBfUvvEGxvaRVk0KaWA== Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8269 Hi Tom, Thank you for your insights. On 9/24/11 9:46 PM, in article wrote: > > If the selector thread's only job is to identify channels which are ready > to be worked on, handing them off to worker threads to be processed, then > it doesn't really have a lot of work of its own to do, and so it's > unlikely to be a bottleneck. > > If you're processing the channels in the selector thread (in which case > it's not really a selector thread), then it could be a bottleneck. So, you > can have several of them; Selector.select() is threadsafe, so with a > little attention to locking, you can have many threads selecting and then > working. This is called the leader/followers model. > > One thing you could consider, if using a single selector thread and a pool > of worker threads, is trying to create some affinity between channels and > threads, so that the same thread always handles the work on a particular > channel (and on several channels); on a multiprocessor machine, this > should increase cache performance, but is not otherwise particularly > valuable. This is not entirely straightforward, because of thread and > channel starvation issues; you should read up on work-stealing if you want > to do that. > This is exactly what I was asking in the OP, this "affinity between channels and threads" as you call it. I didn't mean a "thread-per-connection model". I will try to find a way to implement this affinity on top of the ThreadPoolExecutor even though it is not very clear from its API that it can be done. Among others, I was following this tutorial and there he overloads a bit the single "Selector Thread" for reading and sending data. AFAIK there is no sample code doing Selector.select() from separate threads. Thank you again. Best regards, Giovanni