Are there any java collection wrappers / collections that fail with multithreaded access?

I am trying to track down some concurrency issues related to collections in a large code base.

I would like to replace all collections / maps with an alternative implementation that throws an exception when a third thread accesses it (or similar, I see several possible strategies that might work). Does anyone know of any libraries / tools / strategies for this?

I was thinking about replacing the search in the entire codebase and just temporarily replacing any link to things like the “new HashMap” with a different version. But maybe there is a better way?

+3
source share
2 answers

You can try combining the HashMap get () and put () methods (or whatever you use) with ReentrantLock:

java.util.concurrent.locks.ReentrantLock



class X {
 private final ReentrantLock lock = new ReentrantLock();

     public void m() { 
         if ( ! lock.tryLock() ) {
            // already locked, hint: lock.isHeldByCurrentThread() ?
         }
         lock.lock();
         try {
           // delegate to wrapped hashMap
         } 
         finally {
           lock.unlock()
         }
     }
+2
source

Since there were no obvious participants, I made my own :

Found 29 potential concurrency issues within three hours on a large code base.

+2
source

Source: https://habr.com/ru/post/1731163/


All Articles