Skip to content

Commit 0f1f106

Browse files
committed
添加ExectorServiceUtils
1 parent 3e12323 commit 0f1f106

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
}

0 commit comments

Comments
 (0)