Concurrent Hash Table Assignment

From CSE231 Wiki
Revision as of 12:55, 7 April 2022 by Cosgroved (talk | contribs) (Created page with "==ConcurrentBucketHashMap== frame|[https://commons.wikimedia.org/wiki/File:Hash_table_5_0_1_1_1_1_1_LL.svg mediawiki hash table image]...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

ConcurrentBucketHashMap

For this section of the assignment you will create a thread safe data structure. You have previously created a non-thread safe Map. Now, with the help of multiple ReentrantReadWriteLocks you will create a thread safe Map that both deals with contention and allows simultaneous gets.

class: ConcurrentBucketHashMap.java Java.png
methods: constructor
getIndex
getBucket
getLock
getEntry
get
put
compute
package: kmer.lab.concurrentbuckethashmap
source folder: student/src/main/java

constructor

method: public ConcurrentBucketHashMap(int initialCapacity) Sequential.svg (sequential implementation only)

initialCapacity can be treated as a hint of how many entries the Map can expect to receive.

private support methods

Circle-information.svg Tip:Use HashUtils.toIndex(key,N)

method: private List<Entry<K, V>> getBucket(Object key) Sequential.svg (sequential implementation only)

method: private ReadWriteLock getLock(Object key) Sequential.svg (sequential implementation only)

method: private static <K, V> Optional<Entry<K, V>> getEntry(List<Entry<K, V>> bucket, Object key) Sequential.svg (sequential implementation only)

public methods

Be sure to make each of these methods thread safe by acquiring the appropriate (read or write) lock for the appropriate bucket.

Which method, lock() or tryLock(), is the appropriate one to use for creating a thread safe data structure such as this?

Whenever you acquire a lock, but sure to leverage try finally and unlock() the lock within the finally block.

method: public V get(Object key) Sequential.svg (thread-safe required)

spec

method: public V put(K key, V value) Sequential.svg (thread-safe required)

spec

method: public V compute(K key, BiFunction<? super K, ? super V, ? extends V> remappingFunction) Sequential.svg (thread-safe required)

spec

advice

Attention niels epting.svg Warning: Do NOT call get() from compute(). It will result in a nonintuitive error. getEntry(bucket, key) gives you everything you need.
Attention niels epting.svg Warning: Do NOT call put() from compute() either. Just do the work within the compute method.
Circle-information.svg Tip: Be sure to make your ConcurrentBucketHashMap thread-safe. If any code is going to access shared mutable data, it needs to do so in a thread-safe manner.