为什么要建立线程池?
在多线程项目中,如果建立的线程过多,反而可能导致运行速度大大减慢,这是由于线程建立所花费的时间和资源都比较多。 所以我们在多线程中必须很好地来管理线程, 在很好利用多线程能“同步工作”的好处之外,更有效地提高程序运行速度。
线程池是什么?
线程池是指具有固定数量的线程组成的一种组件。这些线程用来循环执行多个应用逻辑。
怎么建立线程池?
线程池主要包括4个部分,它们是: 1. 线程管理
主要是用来建立,启动,销毁工作线程和把工作任务加入工作线程。
2. 工作线程
它是真正的线程类,运行工作任务。
3. 工作队列
它是用来封装线程的容器。
4. 工作任务
它是实现应用逻辑的具体类。
线程管理类:
import java.util.ArrayList; import java.util.List; import java.util.Queue; import java.util.concurrent.ConcurrentLinkedQueue; /** * ThreadPoolManager.java * * Copyright (C) 2008 State Street Corporation. All Rights Reserved. * */ /** * the thread pool manager, is responsible for starting and stopping the work thread. * * @author e458487 * @version 1.0 */ public class ThreadPoolManager { private static final int DEFAULT_POOL_SIZE = 4; private List<WorkThread> threadPool; private Queue<Task> taskQueue; private int poolSize; public ThreadPoolManager() { this(DEFAULT_POOL_SIZE); } public ThreadPoolManager(int poolSize) { if(poolSize <= 0) { this.poolSize = DEFAULT_POOL_SIZE; }else { this.poolSize = poolSize; } threadPool = new ArrayList<WorkThread>(this.poolSize); taskQueue = new ConcurrentLinkedQueue<Task>(); startup(); } public void startup() { System.out.println("start work thread..."); synchronized(taskQueue) { for(int i = 0; i < this.poolSize; i++) { WorkThread workThread = new WorkThread(taskQueue); threadPool.add(workThread); workThread.start(); } } } public void shutdown() { System.out.println("shutdown work thread..."); synchronized(taskQueue) { for(int i = 0; i < this.poolSize; i++) { threadPool.get(i).shutdown(); } System.out.println("done..."); } } public void addTask(Task task) { synchronized(taskQueue) { taskQueue.add(task); taskQueue.notify(); } } }
工作线程类:
import java.util.Queue; /** * WorkThread.java * * Copyright (C) 2008 State Street Corporation. All Rights Reserved. * */ /** * the work thread used pull the task of task queue, and execute it. * * @author e458487 * @version 1.0 */ public class WorkThread extends Thread { private boolean shutdown = false; private Queue<Task> queue; public WorkThread(Queue<Task> queue) { this.queue = queue; } public void run() { while(!shutdown) { try { Thread.sleep(1000); } catch (InterruptedException e1) { e1.printStackTrace(); } System.out.println(Thread.currentThread() + " is running..."); synchronized(queue) { if(!queue.isEmpty()) { Task task = queue.poll(); task.execute(); }else { try { queue.wait(1000); System.out.println(Thread.currentThread() + " wait..."); }catch(InterruptedException e) { } } } } } public void shutdown() { shutdown = true; } }
工作任务接口:
/** * Task.java * * Copyright (C) 2008 State Street Corporation. All Rights Reserved. * */ /** * The task want to execute. * * @author e458487 * @version 1.0 */ public interface Task { public void execute(); }
工作任务类:
/** * SimpleTask.java * * Copyright (C) 2008 State Street Corporation. All Rights Reserved. * */ /** * @author e458487 * @version 1.0 */ public class SimpleTask implements Task { /* (non-Javadoc) * @see Task#execute() */ public void execute() { System.out.println(Thread.currentThread()); } }
线程池测试类: