public void addListener ( final CancellationListener cancellationListener , final Executor executor ) { checkNotNull ( cancellationListener , "cancellationListener" ) ; checkNotNull ( executor , "executor" ) ; if ( canBeCancelled ( ) ) { ExecutableListener executableListener = new ExecutableListener ( executor , cancellationListener ) ; synchronized ( this ) { if ( isCancelled ( ) ) { executableListener . deliver ( ) ; } else { if ( listeners == null ) { // Now that we have a listener we need to listen to our parent so // we can cascade listener notification. listeners = new ArrayList <> ( ) ; listeners . add ( executableListener ) ; if ( cancellableAncestor != null ) { cancellableAncestor . addListener ( parentListener , DirectExecutor . INSTANCE ) ; } } else { listeners . add ( executableListener ) ; } } } } }
Add a listener that will be notified when the context becomes cancelled .