A quick (I'll try, anyway) reply to both Jon S and John McN's posts on
thread syncing, particulary with respect to the Loaders. Boy, my 1 line BTW
kinda went off like a grenade ;)
The use of the hashtables in the Loaders looks more or less like this - a
very common pattern:
if ( this.containsKey( name ) )
{
obj = this.get( name );
}
else
{
obj = new Object();
this.put( name, obj );
}
The above code is kinda 'trivially' not thread safe because it checks for a
key in the hash and then tries to grab a handle to the associated object
without syncing across the two method calls. If the key is not in the hash
then two threads could decide at the same time that the key/object is not
there, both fall into the ELSE and both put copies of the object into the
hash. Only one copy will survive, obviously. Classic race condition. This
scenario might not cause any overt problems in one of the Loaders but could
be a problem in other usages, e.g. what if the cached object is written with
the assumption that all clients of the system have to access the same
object? This would be a problem even if the hash itself were thead-safe.
Now, Jon's right: the 1.2 Hashtable documentation asserts that 'Hashtable is
synchronized' (although it doesn't seem to do this by syncing the public
methods...?) Otherwise, the bigger problem would be that you could not
guarantee the state of the hash if one thread were in containsKey( ... )
while another thread was adding a new object with put( ... ). This could
cause totally unpredictable results. Again, you need to sync across calls.
Looks like in 1.2 they might have made hashes more thread-safe by default
which'd be a great thing. Needs some research, tho.
Guys, my intent was never to assert that there were general problems with
the Loaders, only that I had noticed a few minor-looking issues that
suggested further review. Just like the connection pool. My experience is
that threading issues can bite you in many, many ways. In code that has to
be thread-safe it's always better to be a little paranoid rather than try to
figure out *if* there could be a problem.
Hope this helps.
PaulO.
------------------------------------------------------------
To subscribe: [EMAIL PROTECTED]
To unsubscribe: [EMAIL PROTECTED]
Search: <http://www.mail-archive.com/turbine%40list.working-dogs.com/>
Problems?: [EMAIL PROTECTED]