-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.java
More file actions
48 lines (40 loc) · 1.64 KB
/
Copy pathTest.java
File metadata and controls
48 lines (40 loc) · 1.64 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
package com.syndemo;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* @author ManhKM on 2/14/2022
* @project Java-String
*/
public class Test {
public static void main(String[] args) {
ThreadPoolExecutor executorService = (ThreadPoolExecutor) Executors
.newFixedThreadPool(10);
StringBuilder stringBuilder = new StringBuilder();
for(int i = 0; i < 10; i++){
executorService.execute(new AppendableRunnable(stringBuilder));
}
shutdownAndAwaitTermination(executorService);
System.out.println("Thread Builder: " + AppendableRunnable.time + " ms | Length: " + stringBuilder.length());
AppendableRunnable.time = 0;
executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(10);
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < 10; i++) {
executorService.execute(new AppendableRunnable(stringBuffer));
}
shutdownAndAwaitTermination(executorService);
System.out.println("Thread Buffer: " + AppendableRunnable.time + " ms | Length: " + stringBuffer.length());
}
static void shutdownAndAwaitTermination(ExecutorService pool) {
pool.shutdown();
try {
if (!pool.awaitTermination(60, TimeUnit.SECONDS)) {
pool.shutdownNow();
if (!pool.awaitTermination(60, TimeUnit.SECONDS))
System.err.println("Pool did not terminate");
}
} catch (Exception e) {
}
}
}