On 12/5/11 2:39 PM, Chad Beaulac wrote:
Looks perfect except one thing. Don't allow clients to put WriteRequest's
into the queue while you're draining it. If you allow other threads to
enqueue more WriteRequest's, the algorithm is unbounded and you risk
getting stuck writing for only one channel. I added a synchronized block
below.
We can avoid the synchronized section by getting the queue size at the
beginning :
Queue<WriteRequest> queue = session.getWriteQueue();
int size = queue.size();
while (size > 0) {
... // process each entry in the queue
size--;
}
But I don't even think it's necessary : a session can only be processed
by one single thread, which is the one which process the write (the
selector's thread).
Of course, if we add an executor somewhere in the chain, this is a
different story, but then, we will need to add some synchronization at
the executor level, not on the selector level, IMO.
--
Regards,
Cordialement,
Emmanuel Lécharny
www.iktek.com