I have a scenario When multiple threads are writing to one LinkedHashMap, on iterating through Map after execution is completed only one key value pair is retained added by last thread.
Please advice me on this.
Thanks
I have a scenario When multiple threads are writing to one LinkedHashMap, on iterating through Map after execution is completed only one key value pair is retained added by last thread.
Please advice me on this.
Thanks
Note that the LinkedHashMap
implementation is not synchronized
. If multiple threads access a linked hash map concurrently, and at least one of the threads modifies the map structurally, it must be synchronized
externally. This is typically accomplished by synchronizing on some object. If no such object exists, the map
should be "wrapped" using the Collections.synchronizedMap
method. This is best done at creation time, to prevent accidental unsynchronized access to the map:
Map m = Collections.synchronizedMap(new LinkedHashMap(...));