Forums Register Login

hashmap of locked records and static

+Pie Number of slices to send: Send
Hi,

I use a hashmap to keep track of the recordslocked
My data class is NOT static but my hashmap IS static.
I synchronized on this hashmap before adding or removing a record in the map.
So I don't need to use a thread safe hash map or vector if I use a static data and static hashmap?
Can somebody confirm that I am right on this?
Thank you very much
- Lydie
+Pie Number of slices to send: Send
Hi Lydie,

Even with a static collection, you still have the potential for two threads to access the collection simultaneously. So you still need thread-safe access.

A good start to this is synchronizing any access to the collection that modifies the collection (which you do). However this does not go far enough - there is still the potential that one thread could try and retrieve data from the collection while another thread is modifying it. If the modification changes the size of the underlying data structure (currently an array) then the thread doing the retrieval could get a NullPointerException or retrieve no data, or retrieve the wrong data, or retrieve the correct data - there is no way of telling what might occur.

So to guard against this you will need to sychronize all access to the collection. You could do this manually (since you already synchronize modification of the collection you only need to synchronize read access to the collection), or you could make the entire collection thread safe (take a look at the synchronizedMap method of java.util.Collections (or any of the other synchronized collections available through the Collections class).

This is one area that there is no "one right way" to handle thread safe access. If your collection access routines are already in sychronized blocks that ensure thread-safe access then you don't need a synchronized collection. But if your collection access is not within a synchronize block then a synchronized collection may save you the effort of synchronizing the access yourself (and keep your code simpler / more readable).

I recommend staying clear of Hashtable and/or Vector though - although they are internally synchronized, it is rarely clear in their usage whether the coder deliberately chose them for that reason, or whether the coder was unaware of other collection classes (or, in real work, whether their use is part of legacy code). In any case a person looking at your code may not realize that you are reliant on the thread-safe behaviour of Hastable or Vector and change it - with disasterous results. Make it explicitly clear by using Collections.synchronizedXxxx or (if you don't need synchronized collections) by using a plain Collection.

Regards, Andrew
Look! It's Leonardo da Vinci! And he brought a tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 755 times.
Similar Threads
Reading Complex Map and Collection
Static Question
Difference between Object and Reference, in this Scenario.
Singleton Thread Safety
Can someone check my lock/unlock methods, please?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 04:26:12.