• Post Reply Bookmark Topic Watch Topic
  • New Topic

Synchronize a method or the object?

 
David Lu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a HashSet that will contain many objects in it. However, several threads will be able add to the set or check to see if it contains a particular object. Obviously, I need to do some sort of synchronization.
Question: Is it better to synchronize the methods that act on the set, or synchronize the HashSet (Collections.synchronizedSet(new HashSet()), or use some other object that is already synchronized?
-David
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That question is impossible to answer without more information on the problem you're trying to solve. Specifically, if your check presence - add operation must be atomic, it needs to happen inside a synchronized block and a synchronized collection might not be very useful.
- Peter
 
David Lu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is a description of the problem.
I have a list of objects (Strings) in a HashSet. This list can be very long (can contain thousands of entries). Several identical threads can be concurrently running (one for each client) who may ask to add an entry to the set (public static void addToList(String x)) or may want to check to see if a note already exists (public static boolean doesExist(String x)). As far as the adds are concerned, I don't want other adds or checks going on at the same time. Now that I think about it, simultaneous checks against the list can occur simultaneously so I suppose the best answer is to only synchronize the add method (and not the check method, nor the HashSet). What do you think?
-David
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David Lu:
Now that I think about it, simultaneous checks against the list can occur simultaneously so I suppose the best answer is to only synchronize the add method (and not the check method, nor the HashSet). What do you think?
Bad idea. You have to synchronize all access; during updates, the internal data structures of your Set are in an inconsistent state and the check method would fail to execute correctly.
The issue is, will a Collections.synchronizedSet() suffice? The issue here is whether the "check - add" action needs to be atomic or not. If it is OK that another thread may add the element between the check and the add, then they don't need to be atomic and a synchronizedSet is fine. If that is not OK, then check and add need to be synchronized on the Set (or an object wrapping the Set) anyway, and you are probably better off with an otherwise unsynchronized Set.
- Peter
 
David Lu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the advice. After posting my reply, I realized myself what a bad idea that was.
Upon further reflection on my code, it is becoming apparent that I should synchronize the methods and not just the set. Thanks again.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must synchronize your adds no matter what you want. You dont have to synchronize your checks, but thats only because they will FAIL if you do an add during your check. So in effect you need your checks synchronized with your adds.
As for check-then-add uninterruptibility, thats logic for your threads to work out. I think this is what the other poster was talking about when he kept mentioning atomicity.
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by CL Gilbert:
You must synchronize your adds no matter what you want. You dont have to synchronize your checks, but thats only because they will FAIL if you do an add during your check.
What do you mean by "FAIL"? The outcome of the check is undefined if you don't synchronize it - it may be correct, it may be a false positive, it may be a false negative. You won't get deterministic behaviour like the ConcurrentModificationException an Iterator would throw under similar circumstances.
- Peter
 
shweta agarwal
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
u should definitely synchronize the methods as they would be a lot more beneficial.though updatons might need a bit more coding but that can surely be handled.
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You won't get deterministic behaviour like the ConcurrentModificationException an Iterator would throw under similar circumstances.
- Peter

You are correct Peter.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!