-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMultiExecutorThread.java
More file actions
63 lines (53 loc) · 2.06 KB
/
MultiExecutorThread.java
File metadata and controls
63 lines (53 loc) · 2.06 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
59
60
61
62
63
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
public class MultiExecutorThread {
public static void main(String args[]) {
System.out.println("Inside :" + Thread.currentThread().getName());
System.out.println(("Creating Executor Service......"));
ExecutorService execute = Executors.newFixedThreadPool(2);
System.out.println("Cretaing Runnable for Multithreading....");
Runnable task1 = ()->{
System.out.println("Executing Task 1 : "+ Thread.currentThread().getName());
try{
TimeUnit.SECONDS.sleep(2);
}
catch(InterruptedException e)
{
throw new IllegalStateException(e);
}
};
Runnable task2 = ()->{
System.out.println("Executing Task 2 : "+ Thread.currentThread().getName());
try{
TimeUnit.SECONDS.sleep(2);
}
catch(InterruptedException e)
{
throw new IllegalStateException(e);
}
};
Runnable task3 = ()->{
System.out.println("Executing Task 3 : "+ Thread.currentThread().getName());
try{
TimeUnit.SECONDS.sleep(2);
}
catch(InterruptedException e)
{
throw new IllegalStateException(e);
}
};
System.out.println("Submitting Tasks.... ");
execute.submit(task1);
execute.submit(task2);
execute.submit(task3);
}
}
// OUTPUT :
// Inside :main
// Creating Executor Service......
// Cretaing Runnable for Multithreading....
// Submitting Tasks....
// Executing Task 2 : pool-1-thread-2
// Executing Task 1 : pool-1-thread-1
// Executing Task 3 : pool-1-thread-2