public static < T > void runWithCallback ( Callable < T > job , final FutureCallback < T > callback ) { final UI ui = UI . getCurrent ( ) ; ListeningExecutorService exec = MoreExecutors . listeningDecorator ( Executors . newSingleThreadExecutor ( ) ) ; ListenableFuture < T > future = exec . submit ( job ) ; if ( callback != null ) { Futures . addCallback ( future , new FutureCallback < T > ( ) { @ Override public void onSuccess ( final T result ) { ui . access ( new Runnable ( ) { @ Override public void run ( ) { callback . onSuccess ( result ) ; } } ) ; } @ Override public void onFailure ( final Throwable t ) { ui . access ( new Runnable ( ) { @ Override public void run ( ) { callback . onFailure ( t ) ; } } ) ; } } ) ; } }
Execute the job in the background and provide a callback which is called when the job is finished .