Hello Adam,

On 6/22/07, Adam Fisk <[EMAIL PROTECTED]> wrote:

I've got a quick question for Maarten and anyone else on the snippet on
threads and IoSession attributes from tutorial on
ProtocolCodecFilter.  The
tutorial reads as follows:

--- start quote --
"We store the state of the decoding process in a session attribute. It
would
also be possible to store this state in the Decoder object itself but this
has several disadvantages:

every IoSession would need its own Decoder instance

MINA ensures that there will never be more than one thread simultaneously
executing the decode() function for the same IoSession, but it does not
guarantee that it will always be the same thread. Suppose the first piece
of
data is handled by thread-1 who decides it cannot yet decode, when the
next
piece of data arrives, it could be handled by another thread. To avoid
visibility problems, you must properly synchronize access to this decoder
state (IoSession attributes are stored in a ConcurrentHashMap, so they are
automatically visible to other threads)."
-- end quote --


If only one thread executes decode() at a time, though, doesn't that mean
the "thread-1" will have already finished its decode() call when more data
arrives and the next thread tries to handle it? If so, I would think any
complicated synchronization issues would not arise because anything that
happens within the decode() method is only happening on one thread at a
time.


It's not because thread-1 has finished its decode call before thread-2
starts handling more data,
that thread-2- will "see" the changes to the decoderState made by thread-1.
This is a consequence of The Java Memory Model (do a google search on this,
or try http://g.oswego.edu/dl/cpj/jmm.html)

I am not very good at explaining it, but fortunately there is an excellent
book on Java Concurrency: see http://jcip.net/

I ask because it seems like each IoSession having its own decoder instance
with its own internal state is a pretty good idea!


I thought so too, until people on this mailing list told me about the
disadvantages of that approach.
Maarten

Am I missing something?

Thanks a lot.

-Adam

Reply via email to