Hi Scott,

This is copy on write.
It works best when modifications are infrequent
compared to access.
e.g. connecting to a jms topic (modification)
and broadcasting messages to the connections (iteration).

Dain's version would copy on every message, rather than
only when a client connects/disconnects.

According to the java language spec

synchronized(clients)
{
...
clients=copy;
}

clients is an expression re-evaluated before entry
into the block.
Once clients is replaced with the copied object
another thread is elected to run.
Any other waiting threads go back to
sleep, waiting on the new version of clients.

Any current iterations effectively use a "snapshot" of
clients from before the modification so don't need
to synchronize (it will never be modified).
But, this means the pattern cannot be used when access
and modification need to see the same version.
It also means you cannot use iterator.remove()

Regards,
Adrian

From: "Scott M Stark" <[EMAIL PROTECTED]>
Reply-To: [EMAIL PROTECTED]
To: <[EMAIL PROTECTED]>
Subject: [JBoss-dev] Why are we using this bogus construct
Date: Thu, 13 Feb 2003 09:20:53 -0800

I have seen this usage construct in a few places in the code and it makes
no sense to me:

class X
{
    HashMap clients = new HashMap();

    public void someMethod()
    {
       synchronized(clients)
        {
            HashMap m = new HashMap(clients);
            m.put(dc, cq);
            clients=m;
       }
        ...
    }
    public void someOtherMethod()
    {
        Iterator i = clients.keySet().iterator();
        while( i.hasNext() )
        {
            ...
        }
    }
}

The unsynchronized clients HashMap is synchronized and copied when
modified and accessed without synchronization in other contexts. This is
not thread safe for the accesses and makes for very expensive updates.
Why isn't the HashMap simply synchronized and used without copying?

xxxxxxxxxxxxxxxxxxxxxxxx
Scott Stark
Chief Technology Officer
JBoss Group, LLC
xxxxxxxxxxxxxxxxxxxxxxxx


-------------------------------------------------------
This SF.NET email is sponsored by: FREE  SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your  SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

_________________________________________________________________
Surf together with new Shared Browsing http://join.msn.com/?page=features/browse&pgmarket=en-gb&XAPID=74&DI=1059



-------------------------------------------------------
This SF.NET email is sponsored by: FREE SSL Guide from Thawte
are you planning your Web Server Security? Click here to get a FREE
Thawte SSL guide and find the answers to all your SSL security issues.
http://ads.sourceforge.net/cgi-bin/redirect.pl?thaw0026en
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
https://lists.sourceforge.net/lists/listinfo/jboss-development

Reply via email to