开发者

About unsynchronized & synchronized access in Java Collections Framework?

开发者 https://www.devze.com 2023-03-14 03:41 出处:网络
Can anyone explain wh开发者_如何学Goat is unsynchronized & synchronized access in Java Collections Framework? Synchronized vs unsynchronized access doesn\'t have to do with the Java Collections Fr

Can anyone explain wh开发者_如何学Goat is unsynchronized & synchronized access in Java Collections Framework?


Synchronized vs unsynchronized access doesn't have to do with the Java Collections Framework per see.

Synchronized access means that you have some sort of locking for accessing the data. This can be introduced by using the synchronized keyword or by using some of the higher level constructs from the java.util.concurrent package.

Unsynchronized access means that you don't have any locking involved when accessing the data.

If you're using a collection in several threads, you better make sure that you're accessing it in a synchronized way, or, that the collection itself is thread safe, i.e., takes care of such locking internally.

To make sure all accesses to some collection coll is accessed in a synchronized way, you can either

  • ...surround accesses with synchronized (coll) { ... }

    public void someMethod() {
        synchronized (coll) {
             // do work...
        }
    }
    
  • ...encapsulate it using Collections.synchronizedCollections

    coll = Collections.synchronizedCollection(coll);
    

In the former approach, you need to make sure that every access to the collection is covered by synchronized. In the latter approach, you need to make sure that every reference points at the synchronized version of the collection.

As pointed out by @Fatal however, you should understand that the latter approach only transforms a thread unsafe collection into a thread safe collection. This is most often not sufficient for making sure that the class you are writing is thread safe. For an example, see @Fatals comment.


Synchronized access means it is thread-safe. So different threads can access the collection concurrently without any problems, but it is probably a little bit slower depending on what you are doing.

Unsynchronized is the opposite. Not thread-safe, but a little bit faster.


The synchronized access in Java Collection Framework is normally done by wrapping with Collections.synchronizedCollection(...) etc. and only access through this wrapper.

There are some exceptions already synchronized like Hashtable and Vector.

But keep in mind: Synchronization is done over the collection instance itself and has a scope for each method call. So subsequent calls maybe interrupted by another thread.

Example: You first call isEmtpy() method getting result that it is not empty and after that you want to retrieve an element from that collection. But this second method call may fail, because collection may be empty now due to actions by another thread done between your calls.

So even with synchronized collections you've to care about synchronization and it maybe necessary to synchronize yourself outside the collection!

0

精彩评论

暂无评论...
验证码 换一张
取 消