-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyThreadPool.java
More file actions
40 lines (36 loc) · 1.11 KB
/
MyThreadPool.java
File metadata and controls
40 lines (36 loc) · 1.11 KB
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
package ThreadPool;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.*;
class CodingTask implements Runnable{
private int aaa;
CodingTask(int i){
aaa = i;
}
@Override
public void run() {
System.out.println(aaa);
}
}
public class MyThreadPool {
public static void main1(String [] args){
ExecutorService executor = Executors.newFixedThreadPool(3);
for(int i = 0; i < 10; i ++){
executor.submit(new CodingTask(i));
}
System.out.println("10 task dispatched successfully");
executor.shutdown();
}
public static void main(String [] args) throws ExecutionException, InterruptedException {
ExecutorService executor = Executors.newFixedThreadPool(3);
List<Future<?>> taskResult = new LinkedList<>();
for(int i = 0; i < 10; i++){
taskResult.add(executor.submit(new CodingTask(i)));
}
System.out.println("10 task dispatched successfully");
for(Future<?> task : taskResult){
task.get();
}
executor.shutdown();
}
}