暂时无话可说,只有代码
// 定义线程池类
class ThreadPool {
// 任务队列
private static BlockingQueue<Runnable> taskQueue;
// 线程集合
private final HashSet<Worker> workers = new HashSet<>();
// 核心线程数
private final int coreSize;
// 获取任务的超时时间
private long timeout;
private TimeUnit timeUnit;
// 执行任务
public void execute(Runnable task) {
// 当任务数没有超过核心线程数直接交个worker对象
// 如果任务数超过了核心线程数,则加入队列
synchronized (workers) {
if (workers.size() < coreSize) {
Worker worker = new Worker(task);
workers.add(worker);
worker.start();
} else {
taskQueue.put(task);
}
}
}
public ThreadPool(int coreSize, long timeout, TimeUnit timeUnit, int queueCapcity) {
this.coreSize = coreSize;
this.timeout = timeout;
this.timeUnit = timeUnit;
taskQueue = new BlockingQueue<>(queueCapcity);
}
// 包装线程集合
class Worker extends Thread {
private Runnable task;
public Worker(Runnable task) {
this.task = task;
}
@Override
public void run() {
// 执行任务
// 1) 当task不为空,执行任务
// 2) 当task执行完毕,再接着从任务队列获取任务并执行
while (task != null || (task = taskQueue.take()) != null) {
try {
task.run();
} catch(Exception e) {
e.printStackTrace();
} finally {
task = null;
}
}
synchronized (workers) {
workers.remove(this);
}
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Worker worker = (Worker) o;
return Objects.equals(task, worker.task);
}
@Override
public int hashCode() {
return Objects.hash(task);
}
}
}
// 自定义消息阻塞队列
class BlockingQueue<T> {
// 1.任务队列,用双端队列进行实现
private final Deque<T> queue = new ArrayDeque<>();
// 2.锁 -- ReentrantLock类似于互斥锁,实现逻辑在AQS中的。AbstractQueueSynchronizer
private final ReentrantLock lock = new ReentrantLock();
// 3.生产者条件变量
private final Condition fullWaitSet = lock.newCondition();
// 4.消费者成员变量
private final Condition emptyWaitSet = lock.newCondition();
// 5.容量
private final int capcity;
public BlockingQueue(int capcity) {
this.capcity = capcity;
}
// 带超时的阻塞获取
public T poll(long timeout, TimeUnit unit) {
// 通过锁来保护,加锁
lock.lock();
try {
// 将超时时间统一转换为纳秒
long nanos = unit.toNanos(timeout);
// 如果队列中没有元素,那么就要阻塞住,不能执行
while (queue.isEmpty()) {
// 让消费者进行等待
try {
// 返回的是剩余的时间
if (nanos <= 0) {
return null;
}
nanos = emptyWaitSet.awaitNanos(nanos);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
T t = queue.removeFirst();
// 要唤醒空列的阻塞队列中等待的
fullWaitSet.signal();
// 获取队列头部信息进行返回, 元素获取完了就应该从队列中移除了
return t;
} finally {
// 释放锁
lock.unlock();
}
}
// 阻塞获取方法
public T take() {
// 通过锁来保护,加锁
lock.lock();
try {
// 如果队列中没有元素,那么就要阻塞住,不能执行
while (queue.isEmpty()) {
// 让消费者进行等待
try {
emptyWaitSet.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
T t = queue.removeFirst();
// 要唤醒空列的阻塞队列中等待的
fullWaitSet.signal();
// 获取队列头部信息进行返回, 元素获取完了就应该从队列中移除了
return t;
} finally {
// 释放锁
lock.unlock();
}
}
// 阻塞添加
public void put(T element) {
// 添加锁
lock.lock();
try {
// 首先判断阻塞队列是否满了
while (queue.size() == capcity) {
// 如果满了,生产者停止生产
try {
fullWaitSet.await();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// 添加新的元素
queue.addLast(element);
// 添加完新元素之后要唤醒阻塞队列中等待的
emptyWaitSet.signal();
} finally {
// 释放锁
lock.unlock();
}
}
// 获取大小
public int size() {
// 先加锁
lock.lock();
try {
return queue.size();
} finally {
// 释放锁
lock.unlock();
}
}
}