private static Runnable rejectionPropagatingRunnable ( final AbstractFuture < ? > outputFuture , final Runnable delegateTask , final Executor delegateExecutor ) { return new Runnable ( ) { @ Override public void run ( ) { final AtomicBoolean thrownFromDelegate = new AtomicBoolean ( true ) ; try { delegateExecutor . execute ( new Runnable ( ) { @ Override public void run ( ) { thrownFromDelegate . set ( false ) ; delegateTask . run ( ) ; } } ) ; } catch ( RejectedExecutionException e ) { if ( thrownFromDelegate . get ( ) ) { // wrap exception? outputFuture . setException ( e ) ; } // otherwise it must have been thrown from a transitive call and the delegate runnable // should have handled it. } } } ; }
Returns a Runnable that will invoke the delegate Runnable on the delegate executor but if the task is rejected it will propagate that rejection to the output future .