我的線程池代碼

(1)根據xml文件來管理線程池的最大最小線程數
(2)對線程池通過Timer定期掃描以防止線程未激活;
(3)通過某一個變量(本程序中是freeThreadCount)來得到空閒線程的數目;

一、配置xml(listen.xml)是:
<?xml version="1.0" encoding="UTF-8"?>
?
???
??? ???? 10?????
??????????? 100???????
??????????? 5?
???
?

二、對於ConsumeThreadPoolPara的javabean:

import java.io.*;
public class ConsumeThreadPoolPara implements Serializable{
? private int minPools;
? private int maxPools;
? private int checkThreadPeriod;

? public int getMinPools(){
??? return minPools;
? }
? public int getMaxPools(){
??? return maxPools;
? }
? public int getCheckThreadPeriod(){
??? return checkThreadPeriod;
? }
? public void setMinPools(int minPools){
??? this.minPools = minPools;
? }
? public void setMaxPools(int maxPools){
??? this.maxPools = maxPools;
? }
? public void setCheckThreadPeriod(int checkThreadPeriod){
??? this.checkThreadPeriod = checkThreadPeriod;
? }
? public String toString(){
??? return minPools+" " + maxPools+" "+checkThreadPeriod;
? }
? public ConsumeThreadPoolPara() {
? }
? public static void main(String[] args) {
??? ConsumeThreadPoolPara consumeThreadPool1 = new ConsumeThreadPoolPara();
? }

}

三、解析xml程序代碼(生成ConsumeThreadPoolPara):
使用jdom解析:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.*;
import java.util.*;

public class ParseConfig {
? static Hashtable Listens = null;
? static ConnPara connpara = null;
? static ConsumeThreadPoolPara consumeThreadPoolPara = null;
? private static String configxml = "listen.xml";

? static{
??? getConsumeThreadPoolPara();? //得到消費的線程池的參數
? }

? /**
?? * 裝載文檔
?? * @return 返回根結點
?? * @throws JDOMException
?? */
? public static Element loadDocument() throws JDOMException{
??? SAXBuilder parser = new SAXBuilder(); // 新建立構造器
??? try {
????? Document document = parser.build(configxml);
????? Element root = document.getRootElement();
????? return root;
??? }catch(JDOMException e){
????? logger.error("listen.xml文件格式非法!");
????? throw new JDOMException();
??? }
? }

? public static ConsumeThreadPoolPara getConsumeThreadPoolPara(){
??? if(consumeThreadPoolPara ==null){
????? try {
??????? Element root = loadDocument();
??????? Element consumeThreadPool = root.getChild("ConsumeThreadPool");
??????? if (consumeThreadPool != null) { //代表有數據庫配置
????????? consumeThreadPoolPara = new ConsumeThreadPoolPara();
????????? Element minPools = consumeThreadPool.getChild("minPools");
????????? consumeThreadPoolPara.setMinPools(Integer.parseInt(minPools.getTextTrim()));
????????? Element maxPools = consumeThreadPool.getChild("maxPools");
????????? consumeThreadPoolPara.setMaxPools(Integer.parseInt(maxPools.getTextTrim()));
????????? Element checkThreadPeriod = consumeThreadPool.getChild("checkThreadPeriod");
????????? consumeThreadPoolPara.setCheckThreadPeriod(Integer.parseInt(checkThreadPeriod.getTextTrim()));
??????? }
????? }
????? catch (JDOMException e) {
????? }
??? }
??? return consumeThreadPoolPara;
? }
}

四、線程池源代碼:
import java.util.*;

/**
?*

Title: 線程池


?*

Description: 採集消費模塊


?*

Copyright: Copyright (c) 2004


?*

Company:


?* @author 張榮斌
?* @version 1.0
?*/

public class ThreadPool {
? private static int minPools = 10; //最小連接池數目
? private static int maxPools = 100; //最大連接池數目
? private static int checkThreadPeriod = 5; //檢查連接池的週期
? ArrayList m_ThreadList;? //工作線程列表
? LinkedList m_RunList = null;? //工作任務列表
? int totalThread = 0;? //總線程數
? static int freeThreadCount = 0;? //未被使用的線程數目
? private java.util.Timer timer = null;? //定時器
? static Object o = new Object();

? static{? //先初始化線程池的參數
??? ConsumeThreadPoolPara consumeThreadPoolPara = ParseConfig.getConsumeThreadPoolPara();
??? if(consumeThreadPoolPara!=null){
????? minPools = consumeThreadPoolPara.getMinPools();
????? maxPools = consumeThreadPoolPara.getMaxPools();
????? checkThreadPeriod = consumeThreadPoolPara.getCheckThreadPeriod()*60*1000;
??? }
? }
? public void setMinPools(int minPools){
??? this.minPools = minPools;
? }
? public void setMaxPools(int maxPools){
??? this.maxPools = maxPools;
? }
? public void setCheckThreadPeriod(int checkThreadPeriod){
??? this.checkThreadPeriod = checkThreadPeriod;
? }
? public ThreadPool() {

??? m_ThreadList=new ArrayList();
??? m_RunList=new LinkedList();
??? for(int i=0;i??????? WorkerThread temp=new WorkerThread();
??????? totalThread = totalThread + 1;
??????? m_ThreadList.add(temp);
??????? temp.start();
??????? try{
????????? Thread.sleep(100);
??????? }catch(Exception e){
??????? }
??? }
??? timer = new Timer(true);? //啓動定時器
??? timer.schedule(new CheckThreadTask(this),0,checkThreadPeriod);
? }

? /**
?? * 當有一個工作來的時候啓動線程池的線程
?? * 1.當空閒線程數爲0的時候,看總線程是否小於最大線程池的數目,就new一個新的線程,否則sleep,直到有空閒線程爲止;
?? * 2.當空閒線程不爲0,則將任務丟給空閒線程去完成
?? * @param work
?? */
? public synchronized void run(String work)
? {
????????? if (freeThreadCount == 0) {
????????????????? if(totalThread??????????????????? WorkerThread temp = new WorkerThread();
??????????????????? totalThread = totalThread + 1;
??????????????????? m_ThreadList.add(temp);
??????????????????? temp.start();
??????????????????? synchronized(m_RunList){
????????????????????? m_RunList.add(work);
????????????????????? m_RunList.notify();
??????????????????? }
????????????????? }else{
??????????????????? while (freeThreadCount == 0) {
????????????????????? try {
??????????????????????? Thread.sleep(200);
????????????????????? }
????????????????????? catch (InterruptedException e) {
????????????????????? }
??????????????????? }
??????????????????? synchronized(m_RunList){
????????????????????? m_RunList.add(work);
????????????????????? m_RunList.notify();
??????????????????? }
????????????????? }
????????? } else {
??????????? synchronized(m_RunList){
????????????? m_RunList.add(work);
????????????? m_RunList.notify();
??????????? }
????????? }
? }

? /**
?? * 檢查所有的線程的有效性
?? */
? public synchronized void checkAllThreads() {

??? Iterator lThreadIterator = m_ThreadList.iterator();

??? while (lThreadIterator.hasNext()) { //逐個遍厲
????? WorkerThread lTestThread = (WorkerThread) lThreadIterator.next();

????? if (! (lTestThread.isAlive())) { //如果處在非活動狀態時
??????? lTestThread = new WorkerThread(); //重新生成個線程
??????? lTestThread.start(); //啓動
????? }
??? }
? }

? /**
?? * 打印調試信息
?? */
? public void printDebugInfo(){
??????? System.out.println("totalThread="+totalThread);
??????? System.out.println("m_ThreadList.size()="+m_ThreadList.size());
}

????? /**
?????? *
?????? *

Title: 工作線程類


?????? * @author 張榮斌
?????? * @version 1.0
?????? */
class WorkerThread extends Thread{
?????????? boolean running = true;
?????????? String work;

??????????? public void run(){
????????????? while(running){
??????????????? synchronized(o){
????????????????? freeThreadCount++;
??????????????? }
??????????????? synchronized(m_RunList){
????????????????? while(m_RunList.size() == 0){
?????????????????????? try{
???????????????????????? m_RunList.wait();
???????????????????????? if(!running) return;
?????????????????????? }catch(InterruptedException e){
?????????????????????? }
????????????????? }
????????????????? synchronized(o){
??????????????????? freeThreadCount--;
????????????????? }
????????????????? work = (String)m_RunList.removeLast();
????????????????? if(work==null) return;
??????????????? }

??// 得到了work 進行工作,這裏work可以換成自己的工作類
????????????? }
??????????? }
}

}
??????? /**
???????? *
???????? *

Title: 定時器調動的任務


???????? * @author 張榮斌
???????? * @version 1.0
???????? */
??????? class CheckThreadTask extends TimerTask{
????????? private static boolean isRunning = false;
????????? private ThreadPool pool;

????????? public CheckThreadTask(ThreadPool pool){
??????????? this.pool = pool;
????????? }
????????? public void run() {
??????????? if (!isRunning)? {
????????????? isRunning = true;
????????????? pool.checkAllThreads();
????????????? isRunning = false;
??????????? }
????????? }
??????? }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章