import java.util.concurrent.*;
import java.util.*;

class MyCallable implements Callable<Integer> {
    public Integer call() {
        try {
            int random = ThreadLocalRandom.current().nextInt(0, 200);
            Thread.sleep(random);
            return random;
        } catch (InterruptedException e) {
            return -1;
        }
    }
}

class Main {
    public static void main(String[] args) {
        ExecutorService es = Executors.newFixedThreadPool(4);
        List<Future<Integer>> futures = new ArrayList<Future<Integer>>();
        
        for (int i = 0; i < 10; i++) {
            futures.add(es.submit(new MyCallable()));
        }
        
        for (int i = 0; i < 10; i++) {
            try {
                Integer j = futures.get(i).get();
                System.out.printf("result from thread %d is %d\n", i, j);
            } catch (ExecutionException | InterruptedException e) {
                System.out.printf("error while reading thread %d\n", i);
            }
        }
    }
}

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: