Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Collections.synchronizedMap - worth using?

Reply
Thread Tools

Collections.synchronizedMap - worth using?

 
 
Ian Pilcher
Guest
Posts: n/a
 
      01-30-2007
I've got a map that will be access by multiple threads, so I'm
considering using Collections.synchronizedMap to "wrap" it. There will
be occasions, though, where I will need to manually synchronize.

synchronized (syncMap)
{
if (syncMap.containsKey(key))
throw new AlreadyGotOneException();
syncMap.put(key, value);
}

This "double synchronization" seems inelegant, and possibly wasteful.

Is there a consensus on the best practice in this situation?

Thanks!

--
================================================== ======================
Ian Pilcher
================================================== ======================
 
Reply With Quote
 
 
 
 
Andreas Leitgeb
Guest
Posts: n/a
 
      02-01-2007
Ian Pilcher <> wrote:
> I've got a map that will be access by multiple threads, so I'm
> considering using Collections.synchronizedMap to "wrap" it. There will
> be occasions, though, where I will need to manually synchronize.


The synchronizedMap will just prevent two actions on the map
to happen at same time:
e.g. when you do a .get(...), at exactly the same time when
another thread tries to do a .put(...,...).

If you need more actions in one go (conglomerate action), then
you still need explicit synchronization just like you wrote:

> synchronized (syncMap)
> {
> if (syncMap.containsKey(key))
> throw new AlreadyGotOneException();
> syncMap.put(key, value);
> }


> This "double synchronization" seems inelegant, and possibly wasteful.


No, it isn't. It's necessary, but with a synchronizedMap you
at least only need to explicitly protect conglomerate actions,
not every single .get() and .put() as you would need for normal
Maps when concurrently accessed in multiple threads.

Re-requesting a lock that the thread already has, shouldn't really
be that expensive.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-01-2007
On Jan 30, 11:18 am, Ian Pilcher <i.pilc...@comcast.net> wrote:
> I've got a map that will be access by multiple threads, so I'm
> considering using Collections.synchronizedMap to "wrap" it. There will
> be occasions, though, where I will need to manually synchronize.
>
> synchronized (syncMap)
> {
> if (syncMap.containsKey(key))
> throw new AlreadyGotOneException();
> syncMap.put(key, value);
> }
>
> This "double synchronization" seems inelegant, and possibly wasteful.
>
> Is there a consensus on the best practice in this situation?
>
> Thanks!

That seems to be a good enough implementation.

I have a few questions, though.
Is the "AlreadyGotOneException" a sort of assertion against programmer
error?
Whether or not it is, does this mean the state of your application is
probably foobar?

If you are simply testing for programmer (or configuration) error,
then it might be better to do this instead:
oldValue = syncMap.put(key, value);
assert oldValue != null;
This has two side effects:
1. No extra sync
2. the new value does replace the old value, but an exception is still
thrown.

Hope this all helps,
Daniel.


 
Reply With Quote
 
Tom Hawtin
Guest
Posts: n/a
 
      02-03-2007
Ian Pilcher wrote:
> I've got a map that will be access by multiple threads, so I'm
> considering using Collections.synchronizedMap to "wrap" it. There will
> be occasions, though, where I will need to manually synchronize.
>
> synchronized (syncMap)
> {
> if (syncMap.containsKey(key))
> throw new AlreadyGotOneException();
> syncMap.put(key, value);
> }
>
> This "double synchronization" seems inelegant, and possibly wasteful.


That probably wont make a particularly large impact on performance. As a
matter of style, if I have to externally synchronise like that, then I
do it everywhere.

A better approach is to use a concurrent map (assuming value cannot be
null).

Value old = map.putIfAbsent(key, value);
if (old != null) {
throw new AlreadyGotOneException();
}

Tom Hawtin
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Worth buying modem router? =?Utf-8?B?QmFkQW5nbGVy?= Wireless Networking 2 07-05-2005 02:33 PM
How much is Bill worth right now? Me_and_you ASP .Net 18 02-12-2005 03:21 AM
Market worth? Nickolas Microsoft Certification 2 12-10-2004 12:06 AM
Is it worth it? Russ McKenna ASP .Net 5 12-06-2003 06:47 PM
MCSE Cert. Is it still worth it? Dominique Feteau Microsoft Certification 3 11-05-2003 08:43 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57