-
Notifications
You must be signed in to change notification settings - Fork 0
/
executorService.java
41 lines (31 loc) · 1.09 KB
/
executorService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.*;
public class executorService {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ExecutorService es = Executors.newFixedThreadPool(5);
List<Integer> list = (new ArrayList<>());
System.out.println("enter 5 integers");
for(int i=0; i<5; i++){
es.execute(new Runnable() {
@Override
public void run(){
synchronized(sc){
System.out.println("enter an integer: ");
int ip = sc.nextInt();
int res = ip*2;
list.add(res);
}
}
});
}
// shutdown the executor
es.shutdown();
while (!es.isTerminated()) {
// waiting for tasks to shutdown
}
System.out.println("double list : " + list);
}
}