private boolean synchronizedRequestPermissionToExecute ( ) { boolean result = false ; synchronized ( this ) { switch ( state . get ( ) ) { case CLOSED : result = true ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Allowing execution in closed state" ) ; } break ; case HALF_OPEN : // Limit the number of concurrent executions when we're half-open if ( halfOpenRunningExecutions < policy . getSuccessThreshold ( ) ) { halfOpenRunningExecutions ++ ; halfOpenLastExecutionStarted = System . nanoTime ( ) ; result = true ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Allowing execution in half-open state. Now running ({0}/{1})" , halfOpenRunningExecutions , policy . getSuccessThreshold ( ) ) ; } } else { // If the user runs an operation which never returns (possible using CompletionStage), we might end up stuck in half-open state // denying all executions forever. For this reason, if we stay in half-open state for longer than the open state delay time, // allow another execution if ( System . nanoTime ( ) - halfOpenLastExecutionStarted > policyDelayNanos ) { halfOpenLastExecutionStarted = System . nanoTime ( ) ; result = true ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Allowing execution in half-open state because enough time has passed without a trial executions completing" ) ; } } else { result = false ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Denying execution in half-open state, trial execution limit reached" ) ; } } } break ; case OPEN : if ( System . nanoTime ( ) - openStateStartTime > policyDelayNanos ) { // We've been in open state for long enough, transition to half-open stateHalfOpen ( ) ; halfOpenRunningExecutions ++ ; halfOpenLastExecutionStarted = System . nanoTime ( ) ; result = true ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Allowing execution because we just changed to half-open state" ) ; } } else { result = false ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "Denying execution in open state" ) ; } } break ; } } return result ; }
Implements the logic for requestPermissionToExecute for the cases where synchronization is required