简体   繁体   中英

About unsynchronized & synchronized access in Java Collections Framework?

任何人都可以在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 , ie, 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!

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