标签:java 线程池
参考博客:http://blog.csdn.net/hsuxu/article/details/8985931
package org.fcl;
import java.util.LinkedList;
import java.util.List;
/**
* 线程池的作用:线程池是为了解决处理器单元内多个线程执行的问题,
* 能够有效的减少CPU的闲置时间,增加处理器单元吞吐能力
* 需要注意的问题:
* 一个线程池一般包含四块内容
* 1、线程池管理器(ThreadPool):用于创建并且管理线程池,包括创建线程池,销毁线程池,添加新的任务
* 2、工作线程(worker):线程池中的线程,没有任务时处于等待的状态,可以循环的执行任务。
* 3、任务接口(task):每个任务都需要实现的额接口, 以供工作线程调度任务的执行,
* 它主要规定了任务的入口,任务执行结束后的收尾工作,任务的执行状态等。
* 4、任务队列(taskQueue):用于存放没有处理的任务线程,提供了一种缓存机制。
* 能理解了这四块的内容,线程池也就该差不多了...
* @author fcl
*
*/
public final class ThreadPool {
private static int worker_num = 5; // 线程池中默认线程个数为5
private WorkThread[] workThreads; // 工作线程
private static volatile int finished_task = 0; // 未处理的任务
private List<Runnable> taskQueue = new LinkedList<Runnable>(); // 任务队列,作为一个缓冲,list不是安全的。
private static ThreadPool threadPool;
/* 默认构造器,调用有参数的构造器 */
private ThreadPool() {
this(5);
}
/* 创建线程池,默认有5个线程 */
private ThreadPool(int worker_num) {
threadPool.worker_num = worker_num;
workThreads = new WorkThread[5];
for (int i = 0; i < worker_num; i++) {
workThreads[i] = new WorkThread();
workThreads[i].start(); // 开启线程池中的线程
}
}
/* 获得线程池,默认为单例模式 */
public static ThreadPool getThreadPool() {
return getThreadPool(threadPool.worker_num);
}
/* 单态模式,获取指定个数线程的线程池 */
public static ThreadPool getThreadPool(int work_num1) {
if (work_num1 < 0) {
work_num1 = worker_num;
}
if (threadPool == null) {
threadPool = new ThreadPool(worker_num);
}
return threadPool;
}
/* 执行任务,其实就是将任务放入任务队列,什么时候执行,由线程池管理器决定 */
public void execute(Runnable task) {
synchronized (taskQueue) {
taskQueue.add(task);
taskQueue.notify();
}
}
/* 执行任务,其实就是将任务放入任务队列,什么时候执行,由线程池管理器决定 */
public void execute(Runnable[] tasks) {
synchronized (taskQueue) {
for (Runnable task : tasks) {
taskQueue.add(task);
taskQueue.notify();
}
}
}
// 批量执行任务,其实只是把任务加入任务队列,什么时候执行有线程池管理器觉定
public void execute(List<Runnable> task) {
synchronized (taskQueue) {
for (Runnable t : task) {
taskQueue.add(t);
taskQueue.notify();
}
}
}
/* 销毁线程池,该方法保证在所有任务都完成的情况下才去销毁线程池,否则的话,等待任务执行完才销毁线程 */
public void destory() {
while (!taskQueue.isEmpty()) {
try {
Thread.sleep(10); // 如果还有任务没有执行完,就休眠10秒
} catch (InterruptedException e) {
e.printStackTrace();
}
/* 工作线程停止,并且置为null */
for (int i = 0; i < worker_num; i++) {
workThreads[i].stopWork();
workThreads[i] = null;
}
threadPool = null; // 将线程池职位null
taskQueue.clear(); // 清空任务队列
}
}
/* 返回工作线程的个数 */
public int getWorkedThreadNum() {
return worker_num;
}
/* 返回已完成任务个数,这里的已完成是指除了任务队列的个数,可能任务并没有实际执行完成 */
public int getFinishedThreadNum() {
return finished_task;
}
/* 返回等待中任务的个数 */
public int getWaitTaskNum() {
return taskQueue.size();
}
// 覆盖toString方法,返回线程池信息:工作线程个数和已完成任务个数
@Override
public String toString() {
return "WorkThread number:" + worker_num + " finished task number:"
+ finished_task + " wait task number:" + getWaitTaskNum();
}
/* 内部类,工作线程 */
private class WorkThread extends Thread {
private boolean isRunning = true;
public void run() {
Runnable r = null;
while (isRunning) {
synchronized (taskQueue) {
if (isRunning && taskQueue.isEmpty()) {
try {
taskQueue.wait(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
if (!taskQueue.isEmpty()) {
r = taskQueue.remove(0);// 取出任务
}
}
// 如果r不为空,就执行任务
if (r != null) {
r.run();
}
finished_task++;
r = null;
}
}
/* 停止工作,让该线程自然执行完run方法,自然结束 */
public void stopWork() {
isRunning = false;
}
}
}
//测试类
package org.fcl;
public class TestThreadPool {
public static void main(String[] args) {
ThreadPool pool = ThreadPool.getThreadPool(3); // 获取有10个线程的线程池
pool.execute(new Runnable[] { new Task(), new Task(), new Task() });
pool.execute(new Runnable[] { new Task(), new Task(), new Task() });
System.out.println(pool);
pool.destory();// 所有线程都执行完成才destory
System.out.println(pool);
}
/*任务类*/
static class Task implements Runnable {
private static volatile int i = 1;
@Override
public void run() {// 执行任务
System.out.println("任务 " + (i++) + " 完成");
}
}
}本文出自 “不羁的风” 博客,请务必保留此出处http://fengcl.blog.51cto.com/9961331/1706572
标签:java 线程池
原文地址:http://fengcl.blog.51cto.com/9961331/1706572