咱们首先实现一个能简单执行任务的线程池,很明显,这样的线程池须要具有两个要素,一个是工做线程,一个是任务队列ide
//工做线程
private List<Thread> workers;
//任务队列
private final BlockingQueue<Runnable> taskQueue;
复制代码
工做线程用于执行具体的每个任务,而任务队列负责接收要执行的任务。this
线程池实例化的时候,肯定线程池工做线程个数poolSize和最大容许接受任务的个数maxAcceptspa
public MyThreadPool(int poolSize, int maxAccept) {
workers = new ArrayList<>(poolSize);
taskQueue = new ArrayBlockingQueue<>(maxAccept);
this.poolSize = poolSize;
this.maxAccept = maxAccept;
}
复制代码
工做线程在线程池的初始化方法中建立和启动线程
public synchronized void init() {
if(!inited) {
for (int i = 0; i < this.poolSize; i++) {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
while (true) {
try {
//从任务队列中取任务,而后执行
Runnable runnable = taskQueue.take();
runnable.run();
} catch (InterruptedException e) {
}
}
}
});
workers.add(t);
t.start();
}
} else {
return;
}
inited = true;
}
复制代码
线程池对外提供执行任务的方法code
public void execute(Runnable runnable) throws RejectException{
if(this.taskQueue.size() < maxAccept) {
this.taskQueue.add(runnable);
} else {
//线程池任务队列满了,抛出拒绝异常
throw new RejectException();
}
}
复制代码
实例化线程池,并调用生命周期
public static void main(String[] args) {
MyThreadPool myThreadPool = new MyThreadPool(5,50);
myThreadPool.init();
try {
myThreadPool.execute(new Runnable() {
@Override
public void run() {
System.out.println("任务被执行了");
}
});
} catch (MyThreadPool.RejectException e) {
}
}
复制代码
以上,咱们实现了一个最简单的线程池队列
按照咱们上一章中提到的,一个完备的线程池除了任务的执行以外,还应该具有如下条件string
显然,本例子中,并无完整地体现线程和线程池的生命周期,咱们实现的还有缺陷,下一章针对本部分进行完善。it