-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPoolApp.java
More file actions
47 lines (36 loc) · 1008 Bytes
/
ThreadPoolApp.java
File metadata and controls
47 lines (36 loc) · 1008 Bytes
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
42
43
44
45
46
47
package examples4;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
class Processor implements Runnable {
private int id;
public Processor(int id) {
this.id = id;
}
@Override
public void run() {
System.out.println("Start process with id " + this.id);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Ended process with id " + this.id);
}
}
public class ThreadPoolApp {
public static void main(String[] args) {
ExecutorService service = Executors.newFixedThreadPool(2);
for (int i = 0; i < 5; i++) {
service.submit(new Processor(i));
}
service.shutdown();
System.out.println("All tasks submited");
try {
service.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Completed all the processes");
}
}