Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
ConcurrentMap and its Implementations | Synchronized Collections
Multithreading in Java
course content

Course Content

Multithreading in Java

Multithreading in Java

1. Multithreading Basics
2. Synchronized Collections
3. High-level Synchronization Mechanisms
4. Multithreading Best Practices

bookConcurrentMap and its Implementations

Real Life Example

A web application uses ConcurrentMap to cache frequently requested data such as user sessions. Different threads can simultaneously update and read data from the map, ensuring fast access and secure operations.

Differences from Other Types

  • Security in a multi-threaded environment: ConcurrentMap automatically handles synchronization of data accesses, while in conventional Map this task must be done manually;
  • Efficiency: Allows reading and writing data in parallel without locking the entire data structure.

ConcurrentMap Implementations

ConcurrentHashMap: Efficiently supports multiple threads by dividing the map into segments (buckets), allowing parallel execution of operations without locking the entire map.

Syntax

java

Main

copy
1
ConcurrentMap<Integer, Integer> concurrentHashMap = new ConcurrentHashMap<>();

ConcurrentHashMap in Java divides data into multiple buckets, each managed by a separate monitor. This setup allows different threads to modify or read data from different buckets simultaneously, which enhances performance.

Threads can access buckets in parallel, reducing locks and avoiding data races.

Each bucket contains records in the form of key-value pairs, which can be organized as linked lists.

ConcurrentSkipListMap: A skip-list based implementation that supports sorted key ordering. Provides fast insertion, deletion, and access to data in a multithreaded environment.

Syntax

java

Main

copy
1
ConcurrentMap<Integer, Integer> concurrentSkipListMap = new ConcurrentSkipListMap<>();

📝Insert: When a new item is added to ConcurrentSkipListMap, it starts at the lowest level. It then moves up through the levels until it is placed where its keys and values are in the correct order.

🔍Search: To find an item by key, ConcurrentSkipListMap begins at the head node of the topmost level. It follows the pointers until it locates a node with a key that is equal to or greater than the search key.

❌Deletion: To delete an item from ConcurrentSkipListMap, it is first removed from the lowest level. It is then downgraded through the levels until it is removed from where its keys and values are correctly ordered.

Example of using ConcurrentMap in code

Main Methods

putIfAbsent(K key, V value): Adds a key-value pair to the map only if the key is not already present.

java

Main

copy
123
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.putIfAbsent("a", 1); // Adds the pair ("a", 1) to the map, as "a" is not already present map.putIfAbsent("a", 2); // Does not change the value, as "a" is already present in the map

remove(Object key, Object value): Removes the key-value pair if the key is mapped to the specified value.

java

Main

copy
1234
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("a", 1); map.remove("a", 1); // Removes the pair ("a", 1), as "a" is mapped to value 1 map.remove("a", 2); // Does nothing, as "a" is not mapped to value 2

replace(K key, V value): Replaces the entry for a key only if it is currently mapped to some value.

java

Main

copy
1234
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("a", 1); map.replace("a", 2); // Replaces the value 1 with 2 for key "a" map.replace("b", 3); // Does nothing, as "b" is not present

compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction): Computes a new value for the specified key using the given remapping function, which might involve creating a new value, modifying, or removing the existing value.

java

Main

copy
1234
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("a", 1); map.compute("a", (k, v) -> v == null ? 1 : v + 1); // Increases the value for key "a" by 1 map.compute("b", (k, v) -> v == null ? 1 : v + 1); // Sets the value to 1 for new key "b"

merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction): Merges the given value with the existing value associated with the key using the provided remapping function, which helps in aggregating data.

java

Main

copy
1234
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("a", 1); map.merge("a", 2, Integer::sum); // Sums the current value (1) with the new value (2), resulting in 3 map.merge("b", 2, Integer::sum); // Sets the value to 2 for new key "b"

getOrDefault(Object key, V defaultValue) - returns the value associated with the specified key, or the default value if the key is not present.

java

Main

copy
1234
ConcurrentMap<String, Integer> map = new ConcurrentHashMap<>(); map.put("a", 1); int value1 = map.getOrDefault("a", 0); // Returns 1, as "a" is present int value2 = map.getOrDefault("b", 0); // Returns 0, as "b" is not present

😔 Limitations

One of the potential drawbacks is order instability, as some implementations might not guarantee the order of elements during iteration. Additionally, there can be limited support for certain operations; for example, atomic conditional updates may not be fully supported in some implementations.

💪 Advantages

On the positive side, high performance is a key benefit, making it well-suited for scenarios involving intensive read and write operations. It also offers ease of use, significantly reducing the need for manual synchronization management in a multi-threaded environment.

1. What is the purpose of ConcurrentMap?
2. Which of the following is a thread-safe implementation of ConcurrentMap?
3. How does ConcurrentHashMap ensure thread-safety?
What is the purpose of ConcurrentMap?

What is the purpose of ConcurrentMap?

Select the correct answer

Which of the following is a thread-safe implementation of ConcurrentMap?

Which of the following is a thread-safe implementation of ConcurrentMap?

Select the correct answer

How does ConcurrentHashMap ensure thread-safety?

How does ConcurrentHashMap ensure thread-safety?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 5
We're sorry to hear that something went wrong. What happened?
some-alt