You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@mina.apache.org by mat <fo...@gmail.com> on 2007/04/21 11:32:02 UTC

IoSession synchronized

Hi folks,

If the code is correct? do I need synchronized (sessions)?

I have a hashtable to maintain the IoSession.

public void onBroadcasting(MyMessage message)
 {
  Enumeration e = sessions.elements();

  while(e.hasMoreElements())
  {
   IoSession session = (IoSession)e.nextElement();
      session.write(message);
  }
 }


However, in mina chat example. Is it neccessary to synchronized( sessions )?


    public void broadcast( String message )
    {
        synchronized( sessions )
        {
            Iterator iter = sessions.iterator();
            while( iter.hasNext() )
            {
                IoSession s = ( IoSession ) iter.next();
                if( s.isConnected() )
                {
                    s.write( "BROADCAST OK " + message );
                }
            }
        }
    }

Re: IoSession synchronized

Posted by Niklas Therning <ni...@trillian.se>.
You are right, ConcurrentHashMap would prevent the
ConcurrentModificationException. Though if a session has been closed and
is removed from the map while you are iterating you could still get it
from the Iterator since ConcurrentHashMap takes a "snapshot" of the map
at the time you call .iterator(). You should deal with that in your
while-loop (e.g. check session.isClosing()) before you write to the session.

/Niklas

mat wrote:
> Thanks for your great explanation. So for performance issue, maybe we
> should
> consider to use concurrentmap in JDK1.5?
>
> 2007/4/23, Niklas Therning <ni...@trillian.se>:
>>
>> mat wrote:
>> > Hi folks,
>> >
>> > If the code is correct? do I need synchronized (sessions)?
>> >
>> > I have a hashtable to maintain the IoSession.
>> >
>> > public void onBroadcasting(MyMessage message)
>> > {
>> >  Enumeration e = sessions.elements();
>> >
>> >  while(e.hasMoreElements())
>> >  {
>> >   IoSession session = (IoSession)e.nextElement();
>> >      session.write(message);
>> >  }
>> > }
>> >
>> >
>> > However, in mina chat example. Is it neccessary to synchronized(
>> > sessions )?
>> >
>> >
>> >    public void broadcast( String message )
>> >    {
>> >        synchronized( sessions )
>> >        {
>> >            Iterator iter = sessions.iterator();
>> >            while( iter.hasNext() )
>> >            {
>> >                IoSession s = ( IoSession ) iter.next();
>> >                if( s.isConnected() )
>> >                {
>> >                    s.write( "BROADCAST OK " + message );
>> >                }
>> >            }
>> >        }
>> >    }
>> >
>>
>> Depends on what Collection you are using to store the sessions. In the
>> chat example we need to synchronize because if a new client connects
>> while we are iterating over the elements we would get a
>> ConcurrentModificationException without the synchronization.
>>
>> Hashtable.iterator() and listIterator() are fail-fast which means that
>> they throw ConcurrentModificationException if the Hashtable is changed
>> while iterating (not changed using the Iterator's methods). The
>> Enumeration returned by Hashtable.elements() is not fail-fast. I think
>> that means that things can go horribly wrong at any time (fast or slow)
>> if the Hashtable is modified. :-)
>>
>> -- 
>> Niklas Therning
>> www.spamdrain.net
>>
>>
>


-- 
Niklas Therning
www.spamdrain.net


Re: IoSession synchronized

Posted by mat <fo...@gmail.com>.
Thanks for your great explanation. So for performance issue, maybe we should
consider to use concurrentmap in JDK1.5?

2007/4/23, Niklas Therning <ni...@trillian.se>:
>
> mat wrote:
> > Hi folks,
> >
> > If the code is correct? do I need synchronized (sessions)?
> >
> > I have a hashtable to maintain the IoSession.
> >
> > public void onBroadcasting(MyMessage message)
> > {
> >  Enumeration e = sessions.elements();
> >
> >  while(e.hasMoreElements())
> >  {
> >   IoSession session = (IoSession)e.nextElement();
> >      session.write(message);
> >  }
> > }
> >
> >
> > However, in mina chat example. Is it neccessary to synchronized(
> > sessions )?
> >
> >
> >    public void broadcast( String message )
> >    {
> >        synchronized( sessions )
> >        {
> >            Iterator iter = sessions.iterator();
> >            while( iter.hasNext() )
> >            {
> >                IoSession s = ( IoSession ) iter.next();
> >                if( s.isConnected() )
> >                {
> >                    s.write( "BROADCAST OK " + message );
> >                }
> >            }
> >        }
> >    }
> >
>
> Depends on what Collection you are using to store the sessions. In the
> chat example we need to synchronize because if a new client connects
> while we are iterating over the elements we would get a
> ConcurrentModificationException without the synchronization.
>
> Hashtable.iterator() and listIterator() are fail-fast which means that
> they throw ConcurrentModificationException if the Hashtable is changed
> while iterating (not changed using the Iterator's methods). The
> Enumeration returned by Hashtable.elements() is not fail-fast. I think
> that means that things can go horribly wrong at any time (fast or slow)
> if the Hashtable is modified. :-)
>
> --
> Niklas Therning
> www.spamdrain.net
>
>

Re: IoSession synchronized

Posted by Niklas Therning <ni...@trillian.se>.
mat wrote:
> Hi folks,
>
> If the code is correct? do I need synchronized (sessions)?
>
> I have a hashtable to maintain the IoSession.
>
> public void onBroadcasting(MyMessage message)
> {
>  Enumeration e = sessions.elements();
>
>  while(e.hasMoreElements())
>  {
>   IoSession session = (IoSession)e.nextElement();
>      session.write(message);
>  }
> }
>
>
> However, in mina chat example. Is it neccessary to synchronized(
> sessions )?
>
>
>    public void broadcast( String message )
>    {
>        synchronized( sessions )
>        {
>            Iterator iter = sessions.iterator();
>            while( iter.hasNext() )
>            {
>                IoSession s = ( IoSession ) iter.next();
>                if( s.isConnected() )
>                {
>                    s.write( "BROADCAST OK " + message );
>                }
>            }
>        }
>    }
>

Depends on what Collection you are using to store the sessions. In the
chat example we need to synchronize because if a new client connects
while we are iterating over the elements we would get a
ConcurrentModificationException without the synchronization.

Hashtable.iterator() and listIterator() are fail-fast which means that
they throw ConcurrentModificationException if the Hashtable is changed
while iterating (not changed using the Iterator's methods). The
Enumeration returned by Hashtable.elements() is not fail-fast. I think
that means that things can go horribly wrong at any time (fast or slow)
if the Hashtable is modified. :-)

-- 
Niklas Therning
www.spamdrain.net