-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestMultiThread.java
More file actions
42 lines (30 loc) · 1.42 KB
/
TestMultiThread.java
File metadata and controls
42 lines (30 loc) · 1.42 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
package multithread;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static java.util.concurrent.TimeUnit.SECONDS;
import java.util.concurrent.TimeUnit;
public class TestMultiThread {
public static void main(String[] args) throws InterruptedException {
ExecutorService service = Executors.newFixedThreadPool(3);
PrintTask printTask1 = new PrintTask('*');
PrintTask printTask2 = new PrintTask('$');
PrintTask printTask3 = new PrintTask('#');
/*
Yah Runnable interface ke object ko submit kar raha hai, jiska run method execute hoga thread ke dwara.
Yaha par ham Callable interface ka use nahi kar rahe hain, kyunki hume return value ki zarurat nahi hai, sirf task ko execute karna hai.
*/
service.submit(printTask1);
service.submit(printTask2);
service.submit(printTask3);
service.shutdown();
System.out.println("All tasks submitted.");
/*
Wait for all tasks to complete or timeout after 10 seconds
awaitTermination ke dwara hum check kar sakte hain ki sabhi tasks complete ho gaye hain ya nahi, agar nahi hue to hum forcefully shutdown kar sakte hain.
*/
if (!service.awaitTermination(10, TimeUnit.SECONDS)) {
System.out.println("Time out. Forcing shutdown...");
service.shutdownNow();
}
}
}