public static void execute(RunnableWithException task, Consumer<Throwable> exceptionHandler, Runnable runFinally, Executor executor) {
        Preconditions.checkNotNull(task, "task");
        Preconditions.checkNotNull(exceptionHandler, "exceptionHandler");
        Preconditions.checkNotNull(runFinally, "runFinally");

        boolean scheduledSuccess = false;
        try {
            executor.execute(() -> {
                try {
                    task.run();
                } catch (Throwable ex) {
                    if (!Exceptions.mustRethrow(ex)) {
                        // Invoke the exception handler, but there's no point in rethrowing the exception, as it will simply
                        // be ignored by the executor.
                        exceptionHandler.accept(ex);
                    }
                } finally {
                    runFinally.run();
                }
            });

            scheduledSuccess = true;
        } finally {
            // Invoke the finally callback in case we were not able to successfully schedule the task.
            if (!scheduledSuccess) {
                runFinally.run();
            }
        }
    }
public static void execute ( RunnableWithException task , Consumer < Throwable > exceptionHandler , Runnable runFinally , Executor executor ) { Preconditions . checkNotNull ( task , "task" ) ; Preconditions . checkNotNull ( exceptionHandler , "exceptionHandler" ) ; Preconditions . checkNotNull ( runFinally , "runFinally" ) ; boolean scheduledSuccess = false ; try { executor . execute ( ( ) -> { try { task . run ( ) ; } catch ( Throwable ex ) { if ( ! Exceptions . mustRethrow ( ex ) ) { exceptionHandler . accept ( ex ) ; } } finally { runFinally . run ( ) ; } } ) ; scheduledSuccess = true ; } finally { if ( ! scheduledSuccess ) { runFinally . run ( ) ; } } }
Executes the given task on the given Executor.

@param task             The RunnableWithException to execute.
@param exceptionHandler A Consumer that will be invoked in case the task threw an Exception. This is not invoked if
the executor could not execute the given task.
@param runFinally       A Runnable that is guaranteed to be invoked at the end of this execution. If the executor
did accept the task, it will be invoked after the task is complete (or ended in failure).
If the executor did not accept the task, it will be executed when this method returns.
@param executor         An Executor to execute the task on.