import java.util.concurrent.Callable;

public class Future<T> implements Runnable {
    private T value = null;
    private Exception exc = null;
    private Callable<T> work;
    private Thread task;
    public Future(Callable<T> w) {
        work = w;
        task = new Thread (this); 
        task.start();
    }
    public void run() {
        try {value = work.call();} 
        catch (Exception e) { exc = e;}
    }
    public T get() throws Exception {
        task.join(); 
        if (exc != null) throw exc;
        return value;
    }
    public static void main(String args[]) {
        // berechne 2 in einem anderen Prozess...
        Future<Integer> f = new Future<Integer>(() -> 2);
        int result=0;
        try { result = f.get(); }
        catch (Exception e) {};
        System.out.println("res: "+result);
    }
}
