How to stop multiple threads in the sling threadpool from executing the same statement?

Viewed 345

How do I prevent threads in the sling threadpool from executing the statement: serv.getService().start( tic, info ); multiple times if the execution has already been completed by one thread?

I tried using a flag, _alreadyExecuted but that does not seem to help.

Log: https://i.stack.imgur.com/x8jZw.png

My code is as follows,

     private void sendBlock( service serv, String tic, Resource block, 
     Update update, Info info, ResourceResolver resolver ) throws 
     IOException {
     private Boolean _alreadyExecuted = Boolean.FALSE;
     if( tic != null ) {
        String tic = null;
        if(!_alreadyExecuted){
             getLogger().info("Before startSubmission. Currently _alreadyexecuted is " +_alreadyExecuted);
             serv.getService().start( tic, info );
             getLogger().info("After startSubmission, _alreadyexecuted is about to change to true. Currently _alreadyexecuted is " +_alreadyExecuted);
             _alreadyExecuted = Boolean.TRUE;
             getLogger().info("After startSubmission, _alreadyexecuted has been changed to true. Currently _alreadyexecuted is " +_alreadyExecuted);
        }
    }
}
1 Answers
Related