1 package de.test.thread.executor.future;
3 import java.util.concurrent.Callable;
4 import java.util.concurrent.ExecutionException;
5 import java.util.concurrent.FutureTask;
6 import java.util.concurrent.TimeUnit;
7 import java.util.concurrent.TimeoutException;
10 public class FutureTaskExample {
14 FutureTask<Car> task = new FutureTask<>(new Callable<Car>() {
17 public Car call() throws Exception {
23 new Thread(task).start();
26 carResult = task.get(1000, TimeUnit.MILLISECONDS);
27 } catch (InterruptedException e) {
28 Thread.currentThread().interrupt();
29 } catch (ExecutionException e) {
30 throw launderThrowable(e);
31 } catch (TimeoutException e) {
32 System.out.println("Timeout");
40 public static class Car {
49 private RuntimeException launderThrowable(final Throwable exception) {
50 exception.printStackTrace();
51 if (exception instanceof RuntimeException)
52 return (RuntimeException)exception;
53 else if (exception instanceof Error)
54 throw (Error)exception;
56 throw new IllegalStateException("Not unchecked", exception);