线程池需要哪些元素:
1、任务队列,做任务的异步提交
2、核心线程数、最大线程数
3、拒绝策略
4、线程工厂
基本上的思路就是,任务队列提交任务,线程通过任务队列获取任务。
任务队列堆积新增工作线程,定期检测活跃线程。 任务满了,提供拒绝策略
下面是一个简单的实现:
package com.puqiuyu.thread.blog;
/**
* 实现一个线程池要哪些东西
* 1、任务队列,做异步提交
* 2、核心线程池、最大线程池
* 3、拒绝策略
* @author sunday
*
*/
import java.util.Iterator;
import java.util.LinkedList;
public class SimpleThreadPool {
private final int INIT_SIZE;
private final static int DEFAULT_SIZE = 10;
private final Object MONITOR = new Object();
private final LinkedList<Runnable> TASK_QUEUE = new LinkedList<>();
private final int MAX_QUEUE_SIZE;
private final static int DEFUALT_QUEUE_SIZE = 50;
private final LinkedList<WorkerThread> WORKERS = new LinkedList<>();
private final int MAX_POOL_SIZE;
private final static int DEFAULT_MAX_SIZE = Integer.MAX_VALUE;
private final RecycleThread recycleThread;
public SimpleThreadPool() {
this(DEFAULT_SIZE, DEFAULT_MAX_SIZE, DEFUALT_QUEUE_SIZE);
}
public SimpleThreadPool(int core, int max, int task) {
this.INIT_SIZE = core;
this.MAX_POOL_SIZE = max;
this.MAX_QUEUE_SIZE = task;
init();
recycleThread = new RecycleThread();
recycleThread.start();
}
private void init() {
for (int i = 0; i < INIT_SIZE; i++) {
createWorkerThread();
}
}
private void createWorkerThread() {
WorkerThread thread = new WorkerThread();
WORKERS.addLast(thread);
}
public void submit(Runnable runnable) {
synchronized (MONITOR) {
// task_queue not empty 说明 线程不够了
if (!TASK_QUEUE.isEmpty() && TASK_QUEUE.size() < MAX_POOL_SIZE) {
createWorkerThread();
}
if (TASK_QUEUE.size() < MAX_QUEUE_SIZE) {
TASK_QUEUE.addLast(runnable);
MONITOR.notifyAll();
}
}
// discard policy
}
public void shutdown() throws InterruptedException {
while (!TASK_QUEUE.isEmpty()) {
Thread.sleep(50);
}
synchronized (MONITOR) {
for (WorkerThread workerThread : WORKERS) {
while (workerThread.status != ThreadStatus.DEAD) {
if (workerThread.status == ThreadStatus.BLOCKED) {
workerThread.interrupt();
workerThread.close();
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
recycleThread.interrupt();
recycleThread.close();
}
}
//工作线程 获取任务执行
private class WorkerThread extends Thread {
ThreadStatus status = ThreadStatus.FREE;
@Override
public void run() {
outer: while (status != ThreadStatus.DEAD) {
Runnable runnable = null;
synchronized (MONITOR) {
while (TASK_QUEUE.size() <= 0) {
status = ThreadStatus.BLOCKED;
try {
MONITOR.wait();
} catch (InterruptedException e) {
continue outer;
}
}
status = ThreadStatus.RUNNING;
runnable = TASK_QUEUE.removeFirst();
}
if (runnable != null) {
status = ThreadStatus.RUNNING;
runnable.run();
status = ThreadStatus.FREE;
}
}
}
private void close() {
this.status = ThreadStatus.DEAD;
}
}
private enum ThreadStatus {
FREE, RUNNING, BLOCKED, DEAD;
}
// 空闲时回收线程
private class RecycleThread extends Thread {
private boolean closed = false;
@Override
public void run() {
while (!closed) {
synchronized (MONITOR) {
if (TASK_QUEUE.isEmpty() && WORKERS.size() > INIT_SIZE) {// 任务队列空,工作队列大于核心线程数
int release = WORKERS.size() - INIT_SIZE;
for (Iterator<WorkerThread> iterator = WORKERS.iterator(); iterator.hasNext();) {
if (release == 0)
break;
WorkerThread thread = iterator.next();
if (thread.status == ThreadStatus.BLOCKED) {
thread.interrupt();
thread.close();
iterator.remove();
release--;
}
}
}
}
try {
Thread.sleep(5_000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void close() {
closed = true;
}
}
}