There are many threads which work on elements on the same map, each with a code, for example:
THREAD_1 works on an element with code "1234"
THREAD_2 works on an element with code "1234"
THREAD_3 works on an element with code "9876"
etc...
Elements are not permanent, i.e. THREAD_1 might remove the element "1234" and then insert it again. What I want is that, while THREAD_1 is working on the element "1234" (also removing it) THREAD_2 must wait.
Is there a way for doing it?
A possible solution might be to insert a fake element in a HashMap and then enforce the synchronization with a "synchronized" clause on that element. What do you think about? (Obviously that fake element would remain in the map also if a thread removed the element with the associated code)...