Return a value from a thread. : ScheduledThreadPoolExecutor « Thread « Java Tutorial






import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
  public static void main(String args[]) throws Exception{
    ExecutorService es = Executors.newFixedThreadPool(3);
    Future<Double> f = es.submit(new Task1());
    Future<Integer> f2 = es.submit(new Task2());

    System.out.println(f.get());
    System.out.println(f2.get());
    es.shutdown();
  }
}

class Task1 implements Callable<Double> {
  Task1() {
  }

  public Double call() {
    return 0.0;
  }
}
class Task2 implements Callable<Integer> {
  Task2() {
  }

  public Integer call() {
    return 1;
  }
}








10.15.ScheduledThreadPoolExecutor
10.15.1.JDK1.5 provides a mechanism to create a pool a scheduled task
10.15.2.Animation with ScheduledThreadPoolExecutorAnimation with ScheduledThreadPoolExecutor
10.15.3.Return a value from a thread.