Class BlockerSync


  • public class BlockerSync
    extends Object
    A utility to help synchronize two threads in cases where one of them is supposed to reach a blocking state before the other may continue.

    Use as follows:

    
     final BlockerSync sync = new BlockerSync();
    
     // thread to be blocked
     Runnable toBeBlocked = () -> {
         // do something, like acquire a shared resource
         sync.blockNonInterruptible();
         // release resource
     }
    
     new Thread(toBeBlocked).start();
     sync.awaitBlocker();
    
     // do stuff that requires the other thread to still hold the resource
     sync.releaseBlocker();