again a question about ArrayList and synchronization.
I just would like to know what exactly this fragment does:
ArrayList<ObjectX> list = ....;
synchronized (list) {
if (list.contains(objectxy) == false) {
list.add(objectxy);
}
}
I have an ArrayList populated with ObjectXs. I want to add an item to the list, but only if the list does not contain the same item. I checked before (in another method) if the list contained an object - there was no result. But it is possible that the two threads at the same time consider that there is no result, and that they are both trying to add objectxy. (there are some other things that must be done between them, which is why I cannot synchronize the whole process)
So, after the process, and when the threads now arrive at the fragment above, I want these two not to add the object to the list. So I thought that when I synchronize access to the list, only one thread can check if it contains this object, and then add it. After this, the second thread can access the list, see that the object is already in it and will no longer add it.
What I want to achieve. Will this work? :-)
So, if so, I'd like to know what exactly this fragment does. Does two threads prevent access to this exact code at the same time? so that code is only available for one thread at a time?
, - ? ( add() s , (), Id , - , ).
ArrayList -, , . , , , ?
, . , . !: -)
[EDIT] , ! , !: -)
? , : -)