1 package examples.example1;
2
3 import java.util.ArrayList;
4 import java.util.List;
5 import java.util.concurrent.ExecutionException;
6 import java.util.concurrent.ExecutorService;
7 import java.util.concurrent.Executors;
8 import java.util.concurrent.Future;
9 import java.util.concurrent.TimeUnit;
10 import java.util.concurrent.atomic.AtomicInteger;
11
12 import examples.example1.classes.MyCaller;
13
14
15 public class Simulation {
16 public static void main(String[] args) throws ExecutionException, InterruptedException {
17 MyCaller myCaller = new MyCaller();
18
19
20
21 ExecutorService executorService = Executors.newWorkStealingPool(10);
22 List<Future<Long>> responses = new ArrayList<>();
23
24
25 long start = System.currentTimeMillis();
26
27 for (int i = 0; i < 100; i++) {
28 AtomicInteger callId = new AtomicInteger(i);
29
30 Future<Long> response = executorService.submit(() -> myCaller.runService(callId.get()));
31 responses.add(response);
32 }
33
34 for (int i = 0; i < responses.size(); i++) {
35 Long response = responses.get(i).get();
36 System.out.println("Response for call #:" + i + " is: " + response);
37 }
38
39
40 executorService.shutdown();
41 executorService.awaitTermination(2, TimeUnit.MINUTES);
42
43 long stop = System.currentTimeMillis();
44 System.out.printf("Finished in %d seconds", (int) (stop - start) / 1000);
45
46 }
47
48 }