once again a question about ArrayList and synchronize.
I'd just like to know what this snippet exactly does:
ArrayList<ObjectX> list = ....;
synchronized (list) {
if (list.contains(objectxy) == false) {
list.add(objectxy);
}
}
Ive got a ArrayList filled with ObjectXs. I want to add then an element to the list, but only if the list doesnt contain the same element. I checked before (in another method) if the list did contain the object - the result was no. But it is possible that two threads at the same time think that the result is no and that they both try then to add objectxy. (there are some other things which must be done inbetween, thats why I cant synchronize the whole process)
So, after the process and when now the threads come to the snippet above, I want to prevent that those two both add the object to the list. So I thought when I synchronize access to the list, only one thread can check if it does contain the object and then add it. After it, the second thread could access the list, see that the object is already in it and would not add it anymore.
Thats what I want to achieve. Would it work? :-)
So, if yes, Id like to know what the snippet exactly does. Does is prevent two threads from accessing this exact code at the same time? so that the code is only available for one thread at the same time?
Or does it lock the list itself for the whole time, for any thread in the application who is at this moment trying to access the list - anywhere? (I dont have other add()s in my code, but many gets(), thats why Id like to know if other threads can access the list and still get elements while another thread is accessing the code above).
The ArrayList itself is a member-variable which is connected with the principal using the application. It is correct that multiple various threads can access the code above at the same time if they are not sent from the same principal, correct?
So, thats what Id like to know. I tried to mark my questions so that its easier to answer them. Thank you for helping! :-)
[EDIT] Thank you for all the answers, which almost all said the same! I think it is clear now! :-)
correct? I hope so :-)
You pretty much have it. The synchronized prevents other threads that lock on the same list object from running their code blocks at the same time. It does not lock the list object itself. Other threads could still access it if they're not also synchronizing on the same object.
The synchronized block guarantees that only one thread may execute this code block, or any other code block which is synchronized on the same object (i.e. the list) at once. For example, if you have
synchronized (list) {
// block A
}
synchronized (list) {
// block B
}
, then if one thread is executing block A, no other thread can be executing block A or block B, because they're both synchronized on the same object. But the list itself isn't locked. Another thread might access the list.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With