废话不多说,直接开始。
一、属性变量
线程的属性变量如下
//线程池是否关闭 private boolean isClosed = false; //任务队列 private LinkedList<Runnable> workQueue; //线程池id private static int threadPoolId; //线程id private int threadId;
二、构造方法
public ThreadPool(int size) { super("ThreadPool-"+(threadPoolId++)); workQueue = new LinkedList<>(); for (int i=0;i<size;i++){ new WorkerThread().start(); } }
构造时传入线程数,初始化任务队列和线程
三、操作方法
1、execute方法
public synchronized void execute(Runnable task) throws Exception { if (isClosed) throw new Exception("ThreadPool is down"); if (task == null) throw new NullPointerException(); workQueue.add(task); //唤醒线程去执行任务 notify(); }
execute方法首先判断判断线程池是否在运行,接着判断任务是否为空,如果两个条件都不满足则将任务加入到任务队列,然后唤醒线程去执行任务
2、getTask方法
public synchronized Runnable getTask() throws InterruptedException { while (workQueue.size() == 0){ if (isClosed) return null; wait(); } return workQueue.removeFirst(); }
如果任务队列为空则阻塞当前线程,如果线程池已关闭则返回null,不然将队头返回。
3、join方法
public void join() { synchronized (this) { isClosed = true; notifyAll(); //唤醒还在getTask()方法中等待任务的工作线程 } Thread[] threads = new Thread[activeCount()]; //enumerate()方法继承自ThreadGroup类,获得线程组中当前所有活着的工作线程 int count = enumerate(threads); for (int i=0; i<count; i++) { //等待所有工作线程运行结束 try { threads[i].join(); //等待工作线程运行结束 }catch(InterruptedException ex) { } } }
join方法是等待线程将工作任务中的任务执行完
4、shutdown方法
public synchronized void shutdown(){ if (!isClosed){ isClosed = true; workQueue.clear(); interrupt(); } }
清空任务队列,中断所有线程
三、工作线程类
private class WorkerThread extends Thread{ public WorkerThread() { //加入到当前ThreadPool线程组中 super(ThreadPool.this,"WorkThread-" + (threadId++)); } @Override public void run() { while (!interrupted()){ Runnable task = null; try { task = getTask(); }catch (Exception e){ } if (task ==null) return; task.run(); } } }
可以看到,当工作线程没有没中断,就去任务队列取一个任务执行。
源代码如下:继承线程组的好处是线程池便于管理线程和使用线程组的方法
public class ThreadPool extends ThreadGroup{ //线程池是否关闭 private boolean isClosed = false; //任务队列 private LinkedList<Runnable> workQueue; //线程池id private static int threadPoolId; //线程id private int threadId; public ThreadPool(int size) { super("ThreadPool-"+(threadPoolId++)); workQueue = new LinkedList<>(); for (int i=0;i<size;i++){ new WorkerThread().start(); } } public synchronized void execute(Runnable task) throws Exception { if (isClosed) throw new Exception("ThreadPool is down"); if (task == null) throw new NullPointerException(); workQueue.add(task); //唤醒线程去执行任务 notify(); } public synchronized Runnable getTask() throws InterruptedException { while (workQueue.size() == 0){ if (isClosed) return null; wait(); } return workQueue.removeFirst(); } public void join() { synchronized (this) { isClosed = true; notifyAll(); //唤醒还在getTask()方法中等待任务的工作线程 } Thread[] threads = new Thread[activeCount()]; //enumerate()方法继承自ThreadGroup类,获得线程组中当前所有活着的工作线程 int count = enumerate(threads); for (int i=0; i<count; i++) { //等待所有工作线程运行结束 try { threads[i].join(); //等待工作线程运行结束 }catch(InterruptedException ex) { } } } public synchronized void shutdown(){ if (!isClosed){ isClosed = true; workQueue.clear(); interrupt(); } } private class WorkerThread extends Thread{ public WorkerThread() { //加入到当前ThreadPool线程组中 super(ThreadPool.this,"WorkThread-" + (++threadId)); } @Override public void run() { while (!interrupted()){ Runnable task = null; try { task = getTask(); }catch (Exception e){ } if (task ==null) return; task.run(); } } } }
本文详细介绍了线程池的设计与实现过程,包括线程池的属性变量、构造方法及核心的操作方法如execute、getTask等。此外还阐述了工作线程类的运作机制。

4773

被折叠的 条评论
为什么被折叠?



