-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVThreadSizeDemo.java
More file actions
71 lines (60 loc) · 2.39 KB
/
Copy pathVThreadSizeDemo.java
File metadata and controls
71 lines (60 loc) · 2.39 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
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class VThreadSizeDemo {
private static Pattern VT_PATTERN = Pattern.compile("(.*)/runnable@(.*)");
public static void main(String[] args) throws InterruptedException {
var max = 10L;
final List<Thread> threads = new ArrayList<>();
final Instant startTime = Instant.now();
final Set<String> poolNames = ConcurrentHashMap.newKeySet();
final Set<String> threadNames = ConcurrentHashMap.newKeySet();
for(var i = 0; i < max; i++){
final var tid = i;
final Thread t = Thread.startVirtualThread(() -> {
try {
final ReentrantLock lock = new ReentrantLock();
lock.lock();
System.out.println("Hi from " + tid + " in " + Thread.currentThread());
Thread.sleep(1000);
System.out.println("Bye from " + tid + " in " + Thread.currentThread());
poolNames.add(getWorkerName());
threadNames.add(getThreadName());
} catch (InterruptedException e) {
e.printStackTrace();
}
});
threads.add(t);
}
for (Thread t : threads) {
t.join();
}
final Instant endTime = Instant.now();
System.out.println("CPU Cores "+ Runtime.getRuntime().availableProcessors());
System.out.println("Workers "+ poolNames.size());
System.out.println("Threads "+ threadNames.size());
System.out.println("Time elapsed "+ Duration.between(startTime, endTime).toString());
}
private static String getWorkerName() {
final String name = Thread.currentThread().toString();
final Matcher matcher = VT_PATTERN.matcher(name);
if (matcher.find()) {
return matcher.group(1);
}
return "";
}
private static String getThreadName() {
final String name = Thread.currentThread().toString();
final Matcher matcher = VT_PATTERN.matcher(name);
if (matcher.find()) {
return matcher.group(2);
}
return "";
}
}