简体   繁体   中英

ArrayList, Threads and synchronize - how does synchronize exactly work for multiple threads

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! :-)

  • the synchronized code block can be only accessed by one of the principals threads. (threads of other principals are not relevant to the speicific principal). the list itself can be accessed at any time from other threads - as long as the access to it is not synchronized with a synchronize-block too. If it is, the thread must wait until it can access the list (that means, that no other thread is at the same time in a synchronized-block)

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 (ie 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.

Yes, only one thread can access that code block at a time. All other threads will wait until thread that got there first will finish execution of the code block.

Also your assumption regarding user principal is correct. If your array list is one per user (principal), then only threads executed with that user (principal) will have to syncronize on that particular array list.

Apart from agreeing that like other answers suggested that it will ONLY block the same block of code. I think the key confusion you have, and most people have is about the lock in the synchronized(lock). You used the list itself as the lock in your case. However, using an object as an lock and if the code in the object will be blocked are totally irrelevant . In fact, you can use any object as the lock as long as it is the same object. That means if you have another member variable named foo , the code below would run essentially the same:

synchronized (foo) {
    if (list.contains(objectxy) == false) {
      list.add(objectxy);
    }
}

Every object can be used as an lock.

It will work as long as all threads synchronize on object before doing anything. Common practice is hiding list form others and giving read only copy.

public class ThreadSafeList {

    private ArrayList<String> list = new ArrayList<String>();


    public synchronized void addUnique(String s) {
        if (!list.contains(s)) {
            list.add(s);
        }
    }


    public synchronized List<String> getList() {
         return Collections.unmodifiableList((new ArrayList<String>(list)));
    }
}

Synchronization is guaranteed by encapsulation.

Synchronized method is similar to

  public void addUnique(String s) 
   synchronized(this){
   list.add(s);
  }

And for java there is no difference on what you sychronize but it is safer to have separate lock object.

You could also use a Vector (a synchronized list) or other synchronized collections. If you are doing many reads but less writes you could use a CopyOnWriteArrayList .

But if you are often doing writes, it produces much overhead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM