-
Notifications
You must be signed in to change notification settings - Fork 18
/
ExecutorServiceConfiguration.java
63 lines (48 loc) · 1.55 KB
/
ExecutorServiceConfiguration.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
package me.chanjar.learning.config;
import org.springframework.context.Lifecycle;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
@Configuration
public class ExecutorServiceConfiguration {
@Bean
public ExecutorService customExecutorService() {
return Executors.newFixedThreadPool(400, new ThreadFactory() {
private final AtomicInteger threadNumber = new AtomicInteger(1);
@Override
public Thread newThread(Runnable r) {
return new Thread(r, "my-executor-service-" + threadNumber.getAndIncrement());
}
});
}
@Bean
public Lifecycle customExecutorServiceLifeCycle() {
final ExecutorService executorService = customExecutorService();
return new Lifecycle() {
@Override
public void start() {
}
@Override
public void stop() {
executorService.shutdown();
boolean successfulTerminated = false;
try {
successfulTerminated = executorService.awaitTermination(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!successfulTerminated) {
executorService.shutdownNow();
}
}
@Override
public boolean isRunning() {
return !executorService.isTerminated();
}
};
}
}