-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFebs.java
More file actions
130 lines (115 loc) · 3.75 KB
/
Copy pathFebs.java
File metadata and controls
130 lines (115 loc) · 3.75 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Copyright (c) 2020 Copyright bp All Rights Reserved.
* Author: pengxiang.li
* Desc:
*/
package cn.brainpoint.febs;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import cn.brainpoint.febs.exception.FebsRuntimeException;
/**
* @author pengxiang.li
*/
public class Febs {
static {
init();
}
private static boolean inited = false;
private static ExecutorService executorService = null;
/**
* The network transfer in fetch style.
*/
public static final Net Net = new Net();
/**
* The utilities: Timer and so on.
*/
public static final Utils Utils = new Utils();
/**
* This thread pool config will use in Promise.
*/
public static class ThreadPoolCfg {
public int corePoolSize = 2;
public int maximumPoolSize = 4;
/**
* in millisecond.
*/
public int keepAliveTime = 20000;
public BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>();
public RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();
public ThreadPoolCfg() {
}
public ThreadPoolCfg(Integer corePoolSize, Integer maximumPoolSize, Integer keepAliveTime,
BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) {
if (maximumPoolSize != null) {
this.maximumPoolSize = maximumPoolSize.intValue();
}
if (keepAliveTime != null) {
this.keepAliveTime = keepAliveTime.intValue();
}
if (corePoolSize != null) {
this.corePoolSize = corePoolSize.intValue();
}
if (workQueue != null) {
this.workQueue = workQueue;
}
if (handler != null) {
this.handler = handler;
}
}
}
/**
* Get a executor service. <i>e.g.</i> <code>
try {
Future<Object> future = Febs.getExecutorService.submit(()->{
// do anything in this thread...
return "any";
});
Object result = future.get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
* </code>
*
* @return get executorService.
*/
public static ExecutorService getExecutorService() {
if (executorService == null) {
throw new FebsRuntimeException("Library uninitialized run \"Febs.init()\" first");
}
return executorService;
}
/**
* Initial
*/
public static void init() {
if (inited) {
return;
}
inited = true;
init(new ThreadPoolCfg());
}
/**
* Initial with thread pool config.
*
* @param threadPoolCfg thread pool config.
*/
public static void init(ThreadPoolCfg threadPoolCfg) {
_initThreadPool(threadPoolCfg);
}
private static void _initThreadPool(ThreadPoolCfg threadPoolCfg) {
if (null != executorService) {
executorService.shutdownNow();
executorService = null;
}
executorService = new ThreadPoolExecutor(threadPoolCfg.corePoolSize, threadPoolCfg.maximumPoolSize,
threadPoolCfg.keepAliveTime, TimeUnit.MILLISECONDS, threadPoolCfg.workQueue, threadPoolCfg.handler);
}
}