forked from spring-templates/spring-concurrency-thread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSpringThreadConcurrencyApplication.java
96 lines (83 loc) · 3.76 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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 org.springframework.context.annotation.Bean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.Executor;
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
@EnableAsync
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 / 1024;
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;
}
@Bean
public Executor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); // Spring에서 사용하는 스레드를 제어한느 설정
executor.setCorePoolSize(50); // thread-pool에 살아있는 thread의 최소 개수
executor.setMaxPoolSize(50); // thread-pool에서 사용할 수 있는 최대 개수
executor.setQueueCapacity(500); //thread-pool에 최대 queue 크기
executor.setThreadNamePrefix("AsyncApp-");
executor.initialize();
return executor;
}
}