Reputation: 8473
I am wondering is there any other way to make a non-synchronized data structure to be thread safe other than using synchronized
data structure like Hashtable
and Vector
, or using a wrapper like Collections.synchronizedList(List<T> arg)
or Collections.synchronizedMap(Map<K,V> arg)
?
I was asked on the interview that how to make a hashmap thread safe, and I told him to use Hashtable
or ConcurrentHashMap
or use the Collections.synchronizedMap
wrapper, however, seems like these answers is not what he is looking for
Upvotes: 5
Views: 688
Reputation: 46219
Yes, you can make it immutable. This is actually an excellent way to make code thread safe in many situations.
Upvotes: 4
Reputation: 54074
Yes. If you do the synchronization of the construct yourself. But this is not recommended for apparent reasons...
Upvotes: 2