Class GridCountDownCallback


  • public class GridCountDownCallback
    extends Object
    Allows to execute callback when a set of operations will be completed. Also has an execution counter, which allows to block callback execution in case if it is below given threshold. By default, threshold is 0 and nothing blocks callback execution.

    Sample usage::

    
         GridCountDownCallback countDownCb = new GridCountDownCallback(
             n,                     //internal counter is initiated with n
             () -> doSomething()    //callback
         );
    
         //each call of countDown() decrements internal counter
         //doSomething() will be executed after counter reaches 0
         for (int i = 0; i < n; i++)
             new Thread(() -> countDownCb.countDown()).start();
     

    Usage with execution threshold::

    
         GridCountDownCallback countDownCb = new GridCountDownCallback(
             n,                     //internal counter is initiated with n
             () -> doSomething(),   //callback
             n/2                    //execution threshold is initiated with n/2
         );
    
         //a half of calls of countDown() increase execution counter, so it reaches threshold and callback executes.
         //doSomething() will be executed after n threads will perform countDown()
         for (int i = 0; i < n; i++)
             new Thread(() -> countDownCb.countDown(n % 2 == 0)).start();
     
    • Constructor Detail

      • GridCountDownCallback

        public GridCountDownCallback​(int initCnt,
                                     Runnable cb,
                                     int executionThreshold)
        Constructor.
        Parameters:
        initCnt - count of invocations of countDown(boolean).
        cb - callback which will be executed after initialCount invocations of countDown(boolean).
        executionThreshold - minimal count of really performed operations to execute callback.
      • GridCountDownCallback

        public GridCountDownCallback​(int initCnt,
                                     Runnable cb)
        Constructor. Execution threshold is set to 0.
        Parameters:
        initCnt - count of invocations of countDown(boolean).
        cb - callback which will be executed after initialCount invocations of countDown(boolean).
    • Method Detail

      • countDown

        public void countDown​(boolean doIncreaseExecutionCounter)
        Decrements the internal counter. If counter becomes 0, callback will be executed.
        Parameters:
        doIncreaseExecutionCounter - whether to increase execution counter
      • countDown

        public void countDown()
        Decrements the internal counter. If counter becomes 0, callback will be executed.