public void myMethod(Object myObject){ synchronize(list){ if (list.contains(myObject){ // do something amazing } } } }
First of all... I understand the potential blocking problem if it takes a long time to "do something amazing". I also see the value of synchronizing when doing a "check-then-do something" action - a compound action. But the main reason I'm asking is that I wonder if the contains() method iterates through the ArrayList. If it does, then I need to lock the list while doing the check. Is that right?
We're pleased to have you here with us here on the ranch, but there are a few rules that need to be followed, and one is that proper names are required. Please take a look at the JavaRanch Naming Policy and adjust your display name to match it.
In particular, your display name must be a first and a last name separated by a space character, and must not be obviously fictitious. You can change it here [ January 15, 2008: Message edited by: Ben Souther ]
[Jim Dewberry]: But the main reason I'm asking is that I wonder if the contains() method iterates through the ArrayList. If it does, then I need to lock the list while doing the check. Is that right?
Yes, contains iterates through the list, and so some synchronization is required here.
You might want to replace the ArrayList with a HashSet or LinkedHashSet - these can have much faster lookup on contains().
A question to consider is: do you need synchronization on the // do something amazing as well, or just on contains()? Because it's pretty simple to rewrite this a little bit:
Now, because of the synchronizedList(), the contains() method is effectively synchronized. But the // do something amazing is not. So, is it possible that right after contains() returns true (and before // do something amazing starts) something might happen which removes myObject from the list? Such that contains() was true a moment ago, but suddenly it's not? And if it's possible, will it create a problem? I can't answer that, because I don't know what something amazing is. But it you determine that either (a) it can't happen, or (b) it can happen but it's not a problem, then go ahead and use the synchronizedList() to synchronize the contains() method but not the something amazing. That will be faster, no blocking. On the other hand if you determine that something amazing must depend on contains() returning true, and the object must not be removed from the list while something amazing is happening, then you need to put a sync block around both contains() and something amazing, as you showed in your code example.
"I'm not back." - Bill Harding, Twister
Jim Dewberry
Greenhorn
Joined: Jan 04, 2008
Posts: 3
posted
0
Wow! Jim!! That was a very thorough and informative answer. Thanks so much! That really helps me.