Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Examples of Using Collections Methods | 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

Examples of Using Collections Methods

Note

Today we will mainly look at methods that help to create thread-safe collections.

Collections Methods

Here are some important methods provided by the class to create synchronized Collections collections:

synchronizedList(List<T> list): Returns a synchronized (thread-safe) list that supports the same contract as the passed list.

java

Main

synchronizedSet(Set<T> set): Returns a synchronized (thread-safe) set that supports the same contract as the passed set.

java

Main

synchronizedMap(Map<K, V> m): Returns a synchronized (thread-safe) map that supports the same contract as the passed map.

java

Main

unmodifiableList(List<? extends T> list): Returns an immutable list containing the same elements as the specified list.

java

Main

unmodifiableMap(Map<? extends K, ? extends V> m): Returns an immutable map containing the same mappings as the specified map.

java

Main

unmodifiableSet(Set<? extends T> set): Returns an immodifiable set containing the same elements as the specified set.

java

Main

What then is the difference between creating synchronized collections through normal implementations (those discussed in the previous chapters) and using Collections methods?

Let's look at the difference using one Map collection as an example

Differences between using ConcurrentHashMap and synchronizedMap()

ConcurrentHashMap employs an advanced method known as lock splitting, which divides the map into segments and synchronizes operations only at the segment level. This approach enables multiple threads to simultaneously read and write data across different segments of the map, improving concurrency and performance.

The method synchronizedMap() wraps the original map into a synchronized proxy object. All methods of this proxy object are synchronized, ensuring thread-safety. However, this approach can reduce performance in a multithreaded environment due to the high locking frequency involved.

For example, if ConcurrentHashMap writes to different buckets, the threads will not block each other. However, if one bucket is accessed by 2 threads, one of them will wait. On the other hand, if the collection is created via synchronizedMap(), only 1 thread will have access to all buckets, and the entire map is blocked.

This means that until one thread finishes its work, other threads will not be able to enter. As a result, operations in ConcurrentHashMap are simply executed faster.

Also in Collections there are Other Methods for Convenient Work with Collections:

Note

The Collections class provides convenient methods for working with collections in Java, simplifying commonly used operations. Synchronized collections created via Collections methods are useful for thread safety, but for more complex multithreaded tasks, it is better to use collections from the java.util.concurrent package.

1. Which method in the Collections class is used to create an immutable list?
2. Which of the following methods in the `Collections` class returns a **thread-safe** map?
3. What is the difference between ConcurrentHashMap and synchronizedMap?

Which method in the Collections class is used to create an immutable list?

Select the correct answer

Which of the following methods in the Collections class returns a thread-safe map?

Select the correct answer

What is the difference between ConcurrentHashMap and synchronizedMap?

Select the correct answer

Everything was clear?

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