private <V> Future<V> executeTask(final Callable<V> callable, final TargetThread targetThread, final Logger logger) {
        try {
            switch (targetThread) {
                // force execution on gui thread
                case GUI_THREAD:
                    if (Platform.isFxApplicationThread()) {
                        return FutureProcessor.completedFuture(callable.call());
                    }

                    FutureTask<V> future = new FutureTask(() -> {
                        try {
                            return callable.call();
                        } catch (Exception ex) {
                            throw ExceptionPrinter.printHistoryAndReturnThrowable(ex, logger);
                        }
                    });
                    Platform.runLater(future);
                    return future;
                // force execution on external thread
                case NON_GUI_THREAD:
                    if (Platform.isFxApplicationThread()) {
                        return GlobalCachedExecutorService.submit(callable);
                    }

                    return FutureProcessor.completedFuture(callable.call());
                default:
                    throw new EnumNotSupportedException(targetThread, this);
            }
        } catch (InterruptedException ex) {
            Thread.currentThread().interrupt();
            return FutureProcessor.canceledFuture(ex);
        } catch (Exception ex) {
            return FutureProcessor.canceledFuture(ex);
        }
    }
private < V > Future < V > executeTask ( final Callable < V > callable , final TargetThread targetThread , final Logger logger ) { try { switch ( targetThread ) { case GUI_THREAD : if ( Platform . isFxApplicationThread ( ) ) { return FutureProcessor . completedFuture ( callable . call ( ) ) ; } FutureTask < V > future = new FutureTask ( ( ) -> { try { return callable . call ( ) ; } catch ( Exception ex ) { throw ExceptionPrinter . printHistoryAndReturnThrowable ( ex , logger ) ; } } ) ; Platform . runLater ( future ) ; return future ; case NON_GUI_THREAD : if ( Platform . isFxApplicationThread ( ) ) { return GlobalCachedExecutorService . submit ( callable ) ; } return FutureProcessor . completedFuture ( callable . call ( ) ) ; default : throw new EnumNotSupportedException ( targetThread , this ) ; } } catch ( InterruptedException ex ) { Thread . currentThread ( ) . interrupt ( ) ; return FutureProcessor . canceledFuture ( ex ) ; } catch ( Exception ex ) { return FutureProcessor . canceledFuture ( ex ) ; } }
Method forces the task execution on the gui/nongui thread.
@param <V> the type of the future
@param callable the task
@param targetThread the target thread where the task will be executed independent of the current thread which has called this method.
@param logger the logger to print logging in error case.
@return a future representing the task result.
@throws CouldNotPerformException is thrown if the task execution has been failed.