-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSpringThreadConcurrencyApplication.java
81 lines (68 loc) · 2.98 KB
/
SpringThreadConcurrencyApplication.java
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
package com.thread.concurrency;
import com.thread.concurrency.counter.batch.BatchCounter;
import com.thread.concurrency.counter.batch.ConcurrentBatchingCounter;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.lang.management.ManagementFactory;
import java.lang.management.MemoryMXBean;
import java.lang.management.MemoryUsage;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;
@SpringBootApplication
public class SpringThreadConcurrencyApplication {
public static void main(String[] args) {
SpringApplication.run(SpringThreadConcurrencyApplication.class, args);
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage initialMemoryUsage = memoryMXBean.getHeapMemoryUsage();
long initialTime = System.currentTimeMillis();
// Run the test
int totalRequest = Integer.MAX_VALUE;
conditionalMultiThreading(totalRequest);
MemoryUsage finalMemoryUsage = memoryMXBean.getHeapMemoryUsage();
long finalTime = System.currentTimeMillis();
long elapsedTime = finalTime - initialTime;
long usedMemory = finalMemoryUsage.getUsed() - initialMemoryUsage.getUsed();
// request with comma
System.out.println("Total request: " + String.format("%,d", totalRequest));
// seconds
System.out.println("Elapsed time: " + elapsedTime / 1000 + " s");
// megabytes
System.out.println("Used memory: " + usedMemory / 1024 / 1024 + " MB");
}
private static void conditionalMultiThreading(int expected) {
BatchCounter counter = new ConcurrentBatchingCounter();
// given
int numberOfThreads = 128;
List<Integer> iterPerThread = range(numberOfThreads, expected);
Consumer<Integer> task = (Integer number) -> {
for (int i = 0; i < number; i++) {
counter.add(1);
}
counter.flush();
};
// when
try (ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor()) {
List<CompletableFuture<Void>> futures = iterPerThread.stream().map(number -> CompletableFuture.runAsync(() -> task.accept(number), executor)).toList();
futures.forEach(CompletableFuture::join);
}
// then
assert expected == counter.show();
}
private static List<Integer> range(int numberOfThreads, int expected) {
int baseValue = expected / numberOfThreads;
int remainder = expected % numberOfThreads;
List<Integer> result = new ArrayList<>();
for (int i = 0; i < numberOfThreads; i++) {
if (i < remainder) {
result.add(baseValue + 1);
} else {
result.add(baseValue);
}
}
return result;
}
}