线程池的模式很像生产者消费者模式,消费的对象是一个一个的能够运行的任务。
在整个过程中,都不需要创建新的线程,而是循环使用这些已经存在的线程。
线程池类:
import java.util.LinkedList; public class ThreadPool { //线程池大小 int threadPoolSize; //容器 LinkedList<Runnable> tasks = new LinkedList<Runnable>(); public ThreadPool() { threadPoolSize = 10; //启动10个线程 synchronized (tasks) { for(int i=0;i<threadPoolSize;i++){ new TaskThread("任务消费者线程 "+i).start(); } } } public void add(Runnable r){ synchronized (tasks) { tasks.add(r); tasks.notifyAll(); } } //成员内部类,方便使用唯一的容器tasks class TaskThread extends Thread { Runnable task; public TaskThread(String name) { super(name); } @Override public void run() { System.out.println("启动: " + this.getName()); while (true) { synchronized (tasks) { while(tasks.isEmpty()){ try { tasks.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } task = tasks.removeFirst(); //synchronized块自然结束或抛出异常时,释放同步对象,故下面这句话可加可不加。 //tasks.notifyAll(); } System.out.println(this.getName() + " 获取到任务,并执行"); task.run(); } } } }测试类:
public class TestThread { public static void main(String[] args){ ThreadPool threadPool = new ThreadPool(); for(int i=0;i<25;i++){ final int j = i; //匿名内部类 Runnable task = new Runnable() { @Override public void run() { System.out.println("working in "+j); try { Thread.sleep(10000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("work accompletion in "+j); } }; threadPool.add(task); try { Thread.sleep(500); } catch (InterruptedException e) { e.printStackTrace(); } } } }测试结果:
启动: 任务消费者线程 2 启动: 任务消费者线程 0 启动: 任务消费者线程 1 启动: 任务消费者线程 4 启动: 任务消费者线程 5 启动: 任务消费者线程 6 启动: 任务消费者线程 3 启动: 任务消费者线程 9 启动: 任务消费者线程 8 启动: 任务消费者线程 7 任务消费者线程 7 获取到任务,并执行 working in 0 任务消费者线程 5 获取到任务,并执行 working in 1 任务消费者线程 8 获取到任务,并执行 working in 2 任务消费者线程 4 获取到任务,并执行 working in 3 任务消费者线程 9 获取到任务,并执行 working in 4 任务消费者线程 6 获取到任务,并执行 working in 5 任务消费者线程 1 获取到任务,并执行 working in 6 任务消费者线程 2 获取到任务,并执行 working in 7 任务消费者线程 0 获取到任务,并执行 working in 8 任务消费者线程 3 获取到任务,并执行 working in 9 work accompletion in 0 任务消费者线程 7 获取到任务,并执行 working in 10 work accompletion in 1 任务消费者线程 5 获取到任务,并执行 working in 11 work accompletion in 2 任务消费者线程 8 获取到任务,并执行 working in 12 work accompletion in 3 任务消费者线程 4 获取到任务,并执行 working in 13 work accompletion in 4 任务消费者线程 9 获取到任务,并执行 working in 14 work accompletion in 5 任务消费者线程 6 获取到任务,并执行 working in 15 work accompletion in 6 任务消费者线程 1 获取到任务,并执行 working in 16 work accompletion in 7 任务消费者线程 2 获取到任务,并执行 working in 17 work accompletion in 8 任务消费者线程 0 获取到任务,并执行 working in 18 work accompletion in 9 任务消费者线程 3 获取到任务,并执行 working in 19 work accompletion in 10 任务消费者线程 7 获取到任务,并执行 working in 20 work accompletion in 11 任务消费者线程 5 获取到任务,并执行 working in 21 work accompletion in 12 任务消费者线程 8 获取到任务,并执行 working in 22 work accompletion in 13 任务消费者线程 4 获取到任务,并执行 working in 23 work accompletion in 14 任务消费者线程 9 获取到任务,并执行 working in 24 work accompletion in 15 work accompletion in 16 work accompletion in 17 work accompletion in 18 work accompletion in 19 work accompletion in 20 work accompletion in 21 work accompletion in 22 work accompletion in 23 work accompletion in 24