假如创建线程的时间开销为T1,线程执行任务的时间为T2,销毁线程的时间为T3,如果T1+T3 >> T2, 这个时候使用线程池往往能大大的提高性能.下面是个线程池的实例.
1. 任务接口Task
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
-
- /**
- *
- * @author fuzuotao
- */
- /* 所有任务的接口 */
- public abstract class Task implements Runnable {
- public long _taskID = 0;
- public Task(){
-
- }
-
- public Task(long task_id){
- _taskID = task_id;
- }
-
- public void setTaskId(long task_id){
- _taskID = task_id;
- }
-
- public long getTaskId(){
- return _taskID;
- }
-
- public void run(){
- System.out.printf("test\r\n");
- }
- }
2. 线程池ThreadPool
- /*
- * To change this template, choose Tools | Templates
- * and open the template in the editor.
- */
- package main;
-
- import java.util.LinkedList;
- import java.util.List;
- import java.util.Collections;
- import org.apache.log4j.Logger;
-
- /**
- *
- * @author fuzuotao
- */
- public class ThreadPool{
- public static long _next_task_id = 0;
- public static final int DEFAULT_THREAD_NUM = 0xa;
- public static final int MAX_THREAD_NUM = 200;
- public int _cur_thread_num = 0;
- public boolean _is_closed = true;
- public List<Task> taskQueue = Collections.synchronizedList(new LinkedList<Task>());
- public WorkThread[] threads;
-
- public static ThreadPool _instance = null;
-
- static Logger logger = Logger.getLogger(ThreadPool.class);
-
- public ThreadPool(){
- _cur_thread_num = DEFAULT_THREAD_NUM;
- threads = new WorkThread[_cur_thread_num];
- for(int i = 0; i < _cur_thread_num; ++ i){
- threads[i] = new WorkThread(i);
- }
- }
-
- public ThreadPool(int thread_num){
- _cur_thread_num = thread_num;
- threads = new WorkThread[_cur_thread_num];
- for(int i = 0; i < _cur_thread_num; ++ i){
- threads[i] = new WorkThread(i);
- }
- }
-
- /* singleton */
- public static ThreadPool getInstance(){
- if(_instance == null){
- synchronized(ThreadPool.class){
- if(_instance == null){
- _instance = new ThreadPool();
- }
- }
- }
- return _instance;
- }
-
- public static long generateTaskId(){
- _next_task_id += (_next_task_id + 1) / 1000000;
- if(_next_task_id == 0) _next_task_id ++;
- return _next_task_id;
- }
-
- public void start(){
- _is_closed = false;
- for(int i = 0; i < _cur_thread_num; ++ i){
- threads[i].start();
- logger.info(String.format("thread [%d] start!", i));
- }
- }
-
- public void close(){
- if(!_is_closed){
- waitforfinish();
- _is_closed = true;
- taskQueue.clear();
- }
- logger.info("Thread pool close!");
- }
-
- public void waitforfinish(){
- synchronized(this){
- _is_closed = true;
- notifyAll();
- }
- for(int i = 0; i < _cur_thread_num; ++ i){
- threads[i].stopThread();
- logger.info(String.format("Thread [%d] stop!", i));
- }
- }
-
- public void addTask(Task new_task){
- if(_is_closed){
- throw new IllegalStateException();
- }
- synchronized(taskQueue){
- if(new_task != null){
- taskQueue.add(new_task);
- taskQueue.notifyAll();
- //taskQueue.notify();
-
- }
- }
- }
-
- public int getTaskCount(){
- return taskQueue.size();
- }
-
- public class WorkThread extends Thread{
- private int _index;
- private boolean _is_running = true;
- public WorkThread(int index){
- _index = index;
- }
-
- public void run(){
- while(_is_running){
- Task t = getTask();
- if(t != null){
- t.run();
- }else{
- // 结束线程
-
- logger.info(String.format("thread [%d] exit", _index));
- return;
- }
- }
- }
-
- public Task getTask(){
- if(_is_closed) return null;
- Task r = null;
- synchronized (taskQueue) {
- while (taskQueue.isEmpty()) {
- try {
- /* 任务队列为空,则等待有新任务加入从而被唤醒 */
- taskQueue.wait();
- } catch (InterruptedException ie) {
- logger.error(PrintStackTrace.getStackTrace(ie));
- }
- }
-
- /* 取出任务执行 */
- r = (Task) taskQueue.remove(0);
- return r;
- }
- }
-
- public void stopThread(){
- _is_running = false;
- try{
- join();
- }catch(InterruptedException ex){
- logger.error(PrintStackTrace.getStackTrace(ex));
- }
- }
- }
-
- }