Are the parallel classes provided by the JDK required to use their own built-in synchronization lock for synchronization?

The JDK provides a set of thread safe classes such as ConcurrentHashMap, ConcurrentLinkedQueue, and AtomicInteger.

Are these classes required for synchronization on thisto implement their thread-safe behavior?

Provided that they can perform our own synchronized operations on these objects and mix them with the built-in ones?

In other words, it is safe:

ConcurrentMap<Integer, Account> accounts 
    = new ConcurrentHashMap<Integer, Account>();

// Add an account atomically
synchronized(accounts) {
    if (!accounts.containsKey(id)) {
        Account account = new Account();
        accounts.put(id, account);
    }
}

And in another thread

// Access the object expecting it to synchronize(this){…} internally
accounts.get(id);

Please note that the simple synchronized block above can probably be replaced with putIfAbsent (), but I can see other cases where synchronizing on an object can be useful.

+3
1

, .

, , , .

, javadoc:

-, concurrency concurrency . Hashtable, -. , , , , . Hashtable , , .

, , , ( ). , , (put get), .

javadoc values ​​():

- " " , throw ConcurrentModificationException, ( ) .

, , : . , ConcurrentModificationExceptions, : , , , .

+6

All Articles