print the return value of Future - Java Thread

Java examples for Thread:ExecutorService

Description

print the return value of Future

Demo Code

package Callable;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

public class Main {
  public static void main(String[] args) {
    ExecutorService executor = Executors.newFixedThreadPool(10);
    List<Future<String>> list = new ArrayList<Future<String>>();
    Callable<String> callable = new MyCallable();
    for (int i = 0; i < 100; i++) {
      Future<String> future = executor.submit(callable);
      list.add(future);// w ww . ja  v a2s .  c om
    }
    for (Future<String> fut : list) {
      try {
        // print the return value of Future, notice the output delay in
        // console
        // because Future.get() waits for task to get completed
        System.out.println(new Date() + "::" + fut.get());
      } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
      }
    }
    // shut down the executor service now
    executor.shutdown();
  }
}

class MyCallable implements Callable<String> {
  @Override
  public String call() throws Exception {
    Thread.sleep(1000);
    return Thread.currentThread().getName();
  }
}

Related Tutorials