-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathThreadPoolExecutorExamples.java
More file actions
97 lines (81 loc) · 2.61 KB
/
Copy pathThreadPoolExecutorExamples.java
File metadata and controls
97 lines (81 loc) · 2.61 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package pool.threads;
import java.util.Date;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy;
import java.util.concurrent.TimeUnit;
public class ThreadPoolExecutorExamples {
public class DemoTask {
final private long i;
public DemoTask(long i) {
this.i = i;
}
public void task() {
System.out.println(Thread.currentThread().getName());
// System.out.println("Hello World " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public class DemoTaskWithThread implements Runnable {
final private long i;
public DemoTaskWithThread(long i) {
this.i = i;
}
public void task() {
System.out.println(Thread.currentThread().getName());
// System.out.println("Hello World " + i);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
task();
}
}
public static void main(String[] args) {
// // Calling the task without any thread i.e with the main thread
// long start = new Date().getTime();
// for (int i = 0; i < 100; i++) {
// DemoTask demoTask = new ThreadPoolExecutorExamples().new DemoTask(i);
// demoTask.task();
// }
// long end = new Date().getTime();
// System.out.println("Total time in ms is :" + (end - start));
long pool1Start = new Date().getTime();
ThreadPoolExecutor pool1 = new ThreadPoolExecutor(5, 15,
500,
TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<>(75),
new CallerRunsPolicy()
);
for (int i = 0; i < 100; i++) {
DemoTaskWithThread demoThread = new ThreadPoolExecutorExamples().new DemoTaskWithThread(i);
pool1.execute(demoThread);
}
pool1.shutdown();
System.out.println("Completed with LinkedBlockingQueue in ms " + (new Date().getTime() - pool1Start));
long startPool2 = new Date().getTime();
ThreadPoolExecutor pool2 = new ThreadPoolExecutor(
5,
15,
500,
TimeUnit.MILLISECONDS,
new SynchronousQueue<Runnable>(),
new CallerRunsPolicy()
);
for(int i = 0; i < 100; i++) {
DemoTaskWithThread demoThread = new ThreadPoolExecutorExamples().new DemoTaskWithThread(i);
pool2.execute(demoThread);
}
pool2.shutdown();
System.out.println("Completed with SynchronousQueue time in ms " + (new Date().getTime() - startPool2));
}
}