Git Product home page Git Product logo

blockingmap4j's People

Contributors

kuniss avatar sarveswaran-m avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

blockingmap4j's Issues

clear does not interrupt blocked threads

Originally raised by @kuniss @ https://sourceforge.net/p/blockingmapforj/discussion/874608/thread/646e5523/?limit=25#5cfd

Hi Sarveswaran,
I very appreciate your work!

I have done some tests and encountered a problem on “clear”. I assume that the clear method does not interrupt all threads already waiting on a “take” call.

Here is my test which fails with a test case time out (AutoResetEvent behaves like the same named class in the .NET framework):

public class Test {
    private static ExecutorService executor;

   @BeforeClass
   public static void startExecutor() {
    executor = Executors.newFixedThreadPool(2);
   }

   @AfterClass
   public static void stopExecutor() {
          executor.shutdown();
   }

   private static final int FAIL_TIMEOUT = 2000;
   private volatile String t;

   @Test(timeout=2*FAIL_TIMEOUT+1000)
   public final void testClearWithRunningTake() {
     final BlockingHashMap<String, String> q = new BlockingHashMap<String, String>();
     t = null;

     final AutoResetEvent are = new AutoResetEvent(false);
     executor.execute(new Runnable() {
       @Override
       public void run() {
          are.setSignaled();
          try {
              t = q.take("some non existent key", Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
              fail("interrupted exception expected due to call to clear");
          } catch (InterruptedException e) {
              // expected exception
          }
              are.setSignaled();
          }
     });

     assertTrue(are.waitOne(FAIL_TIMEOUT)); // wait for background tread was started
     sleep(500); // give background thread some more time to ensure take is really waiting
     q.clear();
     assertTrue(are.waitOne(FAIL_TIMEOUT)); // wait on background thread is finished
     assertNull(t);
   }
    
}

I also took a look into the “clear” code and I assume that the lock is blocking when the waiting threads had already acquired the lock. See my notes below in the code:


public void clear() {
    //clear the map only if it has not been cleared yet
    if (!cleared.getAndSet(true)) {
        primaryMapWriteLock.lock(); // DK: the lock comes too early as it may be locked already at a “take” call
        try {
            for (Thread thread : blockedThreadsMap.keySet()) {
                thread.interrupt();
            }
            primaryMap.clear();
        } finally {
            primaryMapWriteLock.unlock();
        }
    }
 }

May be the following is more sufficient (I did not test it yet):

public void clear() {
    //clear the map only if it has not been cleared yet
    if (!cleared.getAndSet(true)) {
        for (Thread thread : blockedThreadsMap.keySet()) { // DK: This will unblock the blocked threads waiting in “take” calls AND one of them at the end will unlock primaryMapWriteLock 
            thread.interrupt();
        }
        primaryMapWriteLock.lock(); // DK: when all threads are interrupted this gets unlocked and this call will succeed
        try {
            primaryMap.clear();
        } finally {
            primaryMapWriteLock.unlock();
        }
    }
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.