Example script :
- Create two Synchronized Sets (s1 and s2)
- Transfer them to two streams (T1 and T2)
- Start threads
T1 run (): while (forever) s1.equals (c2)
T2 run (): while (forever) s2.equals (s1)
What's happening? - SynchronizedSet gets lock on itself equally
It calculates the length of the passed parameter, as well as what it contains, to determine if it is [Note: this is an assumption based on the logs being analyzed]
If the passed in param is also a SynchronizedSet, calls to size () and containsAll () imply that a lock must also be obtained.
In the above example, the blocking of capture orders for T1 and T2 is as follows:
T1: s1 → s2 T2: s2 → s1
Ofc, this leads to a deadlock.
This issue is not specific to synchronous collections only. This can even happen with a Hashtable or Vector.
I believe this is a limitation of the Java API (design). How to overcome this? How can I make sure this does not happen in my application? Is there some design principle that I must follow without getting into this situation?
user188223
source
share