Name: _____________________________ 6 Digit StudentID: ___ ___ ___ ___ ___ ___

Worksheet 20: ReadWriteLocks and ConcurrentMaps

The documentation for java.util.concurrent.locks.ReadWriteLock states:

A ReadWriteLock maintains a pair of associated locks, one for read-only operations and one for writing. The read lock may be held simultaneously by multiple reader threads, so long as there are no writers. The write lock is exclusive.

For the following methods on a ConcurrentMap, which mutate the current instance and which lock should be acquired, if any. Feel free to review your NotThreadSafeHashTable implementation, if necessary.

compute

V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)
Attempts to compute a mapping for the specified key and its current mapped value (or null if there is no current mapping).

Does the compute method mutate the current instance? Yes, No

Which lock should compute acquire? Read, Write, Neither

computeIfAbsent

V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)
If the specified key is not already associated with a value, attempts to compute its value using the given mapping function and enters it into this map unless null.

Does the computeIfAbsent method mutate the current instance? Yes, No

Which lock should computeIfAbsent acquire? Read, Write, Neither

get

V get(Object key)
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

Does the get method mutate the current instance? Yes, No

Which lock should get acquire? Read, Write, Neither

put

V put(K key, V value)
Maps the specified key to the specified value in this table.

Does the put method mutate the current instance? Yes, No

Which lock should put acquire? Read, Write, Neither

2 Consider the approaches below for a thread safe ConcurrentMap.

2a) locking the entire map:


2b) locking the bucket for the specified key:


2c) locking the entry


3 Why do you think the WebSTAC goes down for scheduled maintenance in the middle of the night?



Post Lecture

Synthesize today's class session

 

 

What is unclear?