I want to have a second lock around a map which is of type Collections.synchronizedMap. How can I have it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Depends what you are trying to do and what code follows.
You should probably
finally unlock(). But then one has to ask why you do not want to use asynchronizedblock instead (see the answer by @WhiteFang34).Also, if all you are doing in the critical section is calling a single get on an already concurrent map, you don’t need the extra lock.
If on the other hand, you do other things, you probably don’t need to use a synchronized map (with its insufficient locking) in the first place (just use your outer lock if you can do so in all places where the map is used).
To sum up, you do not want to use ReadWriteLock when a synchronized block will do, and you do not want to use two locks where one will do (use either SynchronizedMap or the application-level lock, but not both).