Demonstrates WeakHashMapSharedDetector catching unsynchronized access to a WeakHashMap.
WeakCacheService uses a WeakHashMap<Object, String> as a shared cache visible to all
threads. WeakHashMap is explicitly not thread-safe: concurrent put() calls can corrupt
the internal hash table, and GC-triggered entry expiry runs concurrently with all mutations.
- Remove
@Disabledfromtest_concurrent_detectsSharedWeakHashMapinWeakCacheServiceTest. - Run the test:
mvn test gradle test - WeakHashMapSharedDetector will report multiple threads accessing the same
WeakHashMapinstance without synchronization.
Replace WeakHashMap with a ConcurrentHashMap backed by WeakReference values,
or wrap access in Collections.synchronizedMap(new WeakHashMap<>()) with external
locking during iteration.