aspose file tools
The moose likes Threads and Synchronization and the fly likes synchronize on .contains() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Threads and Synchronization
Reply Bookmark "synchronize on .contains() ?" Watch "synchronize on .contains() ?" New topic
Author

synchronize on .contains() ?

Jim Dewberry
Greenhorn

Joined: Jan 04, 2008
Posts: 3
Should I do this:

public class MyClass {

public ArrayList list = new ArrayList();

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?

Thanks!
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

"berry doo",
Welcome to JavaRanch!

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 ]

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Jim Dewberry
Greenhorn

Joined: Jan 04, 2008
Posts: 3
Sorry about that. I changed my profile so you can see my name.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

Thanks
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
[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
Wow! Jim!! That was a very thorough and informative answer. Thanks so much! That really helps me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: synchronize on .contains() ?
 
Similar Threads
easymock - matcher
generic casting with object.getClass() ?
Map key or key in object?
set Object direct or indirect?
about variable persistence...