forked from THEONE10211024/ThreadPool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThreadPoolManager(java 线程池).java
125 lines (104 loc) · 2.88 KB
/
ThreadPoolManager(java 线程池).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
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
package com.theone.android.lib.commons;
import java.util.LinkedList;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
/**
* 线程池管理(线程统一调度管理)
*/
public final class ThreadPoolManager {
private static ThreadPoolManager sThreadPoolManager = new ThreadPoolManager();
// 线程池维护线程的最少数量
private static final int SIZE_CORE_POOL = 3;
// 线程池维护线程的最大数量
private static final int SIZE_MAX_POOL = 4;
// 线程池维护线程所允许的空闲时间
private static final int TIME_KEEP_ALIVE = 5000;
// 线程池所使用的缓冲队列大小
private static final int SIZE_WORK_QUEUE = 500;
// 任务调度周期
private static final int PERIOD_TASK_QOS = 1000;
/*
* 线程池单例创建方法
*/
public static ThreadPoolManager newInstance() {
return sThreadPoolManager;
}
// 任务缓冲队列
private final Queue<Runnable> mTaskQueue = new LinkedList<Runnable>();
/*
* 线程池超出界线时将任务加入缓冲队列
*/
private final RejectedExecutionHandler mHandler = new RejectedExecutionHandler() {
@Override
public void rejectedExecution(Runnable task, ThreadPoolExecutor executor) {
mTaskQueue.offer(task);
}
};
/*
* 将缓冲队列中的任务重新加载到线程池
*/
private final Runnable mAccessBufferThread = new Runnable() {
@Override
public void run() {
if (hasMoreAcquire()) {
mThreadPool.execute(mTaskQueue.poll());
}
}
};
/*
* 创建一个调度线程池
*/
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
/*
* 通过调度线程周期性的执行缓冲队列中任务
*/
protected final ScheduledFuture<?> mTaskHandler = scheduler.scheduleAtFixedRate(mAccessBufferThread, 0,
PERIOD_TASK_QOS, TimeUnit.MILLISECONDS);
/*
* 线程池
*/
private final ThreadPoolExecutor mThreadPool = new ThreadPoolExecutor(SIZE_CORE_POOL, SIZE_MAX_POOL,
TIME_KEEP_ALIVE, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(SIZE_WORK_QUEUE), mHandler);
/*
* 将构造方法访问修饰符设为私有,禁止任意实例化。
*/
private ThreadPoolManager() {
}
public void perpare() {
if (mThreadPool.isShutdown() && !mThreadPool.prestartCoreThread()) {
@SuppressWarnings("unused")
int startThread = mThreadPool.prestartAllCoreThreads();
}
}
/*
* 消息队列检查方法
*/
private boolean hasMoreAcquire() {
return !mTaskQueue.isEmpty();
}
/*
* 向线程池中添加任务方法
*/
public void addExecuteTask(Runnable task) {
if (task != null) {
mThreadPool.execute(task);
}
}
protected boolean isTaskEnd() {
if (mThreadPool.getActiveCount() == 0) {
return true;
} else {
return false;
}
}
public void shutdown() {
mTaskQueue.clear();
mThreadPool.shutdown();
}
}