![]() |
In Java, a synchronized HashTable is achieved by wrapping a regular HashTable with the Collection.synchronizedMap( ) method. This wrapper ensures that each method of the Map interface is synchronized, making the HashTable thread-safe. Syntax:Map<KeyType, ValueType> synchronizedHashTable = Collections.synchronizedMap(new HashTable<>());
Parameters:
Approaches:
Program to Create a Synchronized HashTable in JavaBelow are the code implementations using the above two approaches. Approach 1: Using Collections.synchronizedMap()Below is the Program to create a synchronized HashTable using Collections.synchronizedMap(). Java
Output
Synchronized HashTable is: one: 1 Synchronized HashTable is: Two: 2 Synchronized HashTable is: Three: 3 Explanation of the above Program:
Approach 2: Using Concurrent ClassBelow is the Program to Create a Synchronized HashTable using Concurrent Class Java
Output
Synchronized HashTable is: one: 1 Synchronized HashTable is: two: 2 Synchronized HashTable is: three: 3 Explanation of the above Program:
|
Reffered: https://www.geeksforgeeks.org
Java |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 16 |