forked from Beerkay/JavaMultiThreading
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.java
More file actions
58 lines (51 loc) · 1.6 KB
/
App.java
File metadata and controls
58 lines (51 loc) · 1.6 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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package ThreadPools_5;
/**
* ThreadPool ("number of workers in a factory")
* <br><br>
* Codes with minor comments are from
* <a href="http://www.caveofprogramming.com/youtube/">
* <em>http://www.caveofprogramming.com/youtube/</em>
* </a>
* <br>
* also freely available at
* <a href="https://www.udemy.com/java-multithreading/?couponCode=FREE">
* <em>https://www.udemy.com/java-multithreading/?couponCode=FREE</em>
* </a>
*
* @author Z.B. Celik <celik.berkay@gmail.com>
*/
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;
}
public void run() {
System.out.println("Starting: " + id);
try {
Thread.sleep(5000);
} catch (InterruptedException ignored) {
}
System.out.println("Completed: " + id);
}
}
public class App {
public static void main(String[] args) {
/**
* Created 2 threads, and assign tasks (Processor(i).run) to the threads
*/
ExecutorService executor = Executors.newFixedThreadPool(2);//2 Threads
for (int i = 0; i < 2; i++) { // call the (Processor(i).run) 2 times with 2 threads
executor.submit(new Processor(i));
}
executor.shutdown();
System.out.println("All tasks submitted.");
try {
executor.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException ignored) {
}
System.out.println("All tasks completed.");
}
}