File tree Expand file tree Collapse file tree 2 files changed +57
-0
lines changed
spring-cloud-gray-utils/src/main/java/cn/springcloud/gray/concurrent Expand file tree Collapse file tree 2 files changed +57
-0
lines changed Original file line number Diff line number Diff line change 1+ package cn .springcloud .gray .concurrent ;
2+
3+ import lombok .AllArgsConstructor ;
4+ import lombok .Builder ;
5+ import lombok .Data ;
6+ import lombok .NoArgsConstructor ;
7+
8+ /**
9+ * @author saleson
10+ * @date 2020-09-23 23:39
11+ */
12+ @ Data
13+ @ NoArgsConstructor
14+ @ AllArgsConstructor
15+ @ Builder
16+ public class ConcurrnetProperties {
17+ private int corePoolSize = 5 ;
18+ private int maximumPoolSize = 20 ;
19+ private long keepAliveTime = 30000 ;
20+ private int queueSize = 10 ;
21+ private String threadPrefix ;
22+ }
Original file line number Diff line number Diff line change 1+ package cn .springcloud .gray .concurrent ;
2+
3+ import cn .springcloud .gray .utils .StringUtils ;
4+
5+ import java .util .concurrent .*;
6+
7+ /**
8+ * @author saleson
9+ * @date 2020-09-23 23:43
10+ */
11+ public class ExectorServiceUtils {
12+
13+ public static ExecutorService createExecutorService (ConcurrnetProperties concurrnetProperties ) {
14+ BlockingQueue <Runnable > workQueue = null ;
15+ if (concurrnetProperties .getQueueSize () == 0 ) {
16+ workQueue = new SynchronousQueue <>();
17+ } else if (concurrnetProperties .getQueueSize () < 0 ) {
18+ workQueue = new LinkedBlockingQueue <>();
19+ } else {
20+ workQueue = new ArrayBlockingQueue <>(concurrnetProperties .getQueueSize ());
21+ }
22+
23+ ThreadFactory threadFactory = StringUtils .isEmpty (concurrnetProperties .getThreadPrefix ()) ?
24+ Executors .defaultThreadFactory () : new DefaultThreadFactory (concurrnetProperties .getThreadPrefix ());
25+
26+ return new ThreadPoolExecutor (
27+ concurrnetProperties .getCorePoolSize (),
28+ concurrnetProperties .getMaximumPoolSize (),
29+ concurrnetProperties .getKeepAliveTime (),
30+ TimeUnit .MILLISECONDS ,
31+ workQueue ,
32+ threadFactory
33+ );
34+ }
35+ }
You can’t perform that action at this time.
0 commit comments