views:

58

answers:

2

I'm trying to track some concurrency problems related to collections in a large code-base.

I would like to replace all the collections/maps with an alternate implementation that throws an exception when the 3rd thread accesses it (or similar; I can see several possible strategis that might work). Anyone know of any libraries/tools/strategies to do this ?

I was thinking about doing a search-replace in the entire code-base and just temporarily replace any reference to stuff like "new HashMap" with a different version. But maybe there's a better way ?

+2  A: 

You could try to wrap the HashMap get() and put() methods (or whatelse 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()
         }
     }
stacker
+1 for a nice solution, but that'd actually require concurrent access to happen. I think my solution would allow me to explore the general concurrent usage a bit better in a large an largely unknown code base
krosenvold
+2  A: 

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

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

krosenvold
+1 ............
Rakesh Juyal