public class ThreadPool { private LinkedList<Runnable> taskList = null; private boolean closed = false; public ThreadPool() { this.taskList = new LinkedList<Runnable>(); for (int i = 0; i < 4; i++) { Thread work = new WorkThread(); work.setDaemon(true); work.start(); } } public synchronized void addTask(Runnable task) { if (closed) return; if (null == task) return; this.taskList.add(task); notify(); } private synchronized Runnable getTask() { while (this.taskList.size() == 0) { try { if (closed) return null; wait(); } catch (InterruptedException e) { e.printStackTrace(); } } return this.taskList.removeFirst(); } public void close() { synchronized (this) { this.closed = true; } } private class WorkThread extends Thread { public void run() { while (true) { Runnable task = null; task = getTask(); if (null == task) return; task.run(); } } } }