// Collection of code snippets by Arne Vajhøj 
// (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) 
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 ThreadFun implements Callable<String> {
    private String sv;
    private int iv;
    public ThreadFun(String sv, int iv) {
        this.sv = sv;
        this.iv = iv;
    }
    @Override
    public String call() throws InterruptedException {
        Thread.sleep(10);
        return sv + iv;
    }
    private static final int POOL_SIZE = 10;
    private static final int N = 100;
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        ExecutorService es = Executors.newFixedThreadPool(POOL_SIZE);
        @SuppressWarnings("unchecked")
        Future<String>[] task = new Future[N];
        for(int i = 0; i < task.length; i++) {
            task[i] = es.submit(new ThreadFun("Test", i));
        }
        for(int i = 0; i < task.length; i++) {
            String res = task[i].get();
            if(!res.equals("Test" + i)) throw new RuntimeException("Ooops");
        }
        es.shutdown();       
    }
}
