您的位置:首页 >> 编程开发 >> Java >> 线程 >> 正文
RSS
 

我的线程池代码

http://www.rdxx.com 02年10月15日 11:49 互联网 我要投稿

关键词: 线程池 , 代码 , 线程

(1)根据xml文件来管理线程池的最大最小线程数
(2)对线程池通过Timer定期扫描以防止线程未激活;
(3)通过某一个变量(本程序中是freeThreadCount)来得到空闲线程的数目;

一、配置xml(listen.xml)是:
<?xml version="1.0" encoding="UTF-8"?>
  <config>
    <ConsumeThreadPool>
         <minPools>10</minPools>      <!--线程池最小线程-->
            <maxPools>100</maxPools>        <!--线程池最大线程-->
            <checkThreadPeriod>5</checkThreadPeriod>  <!--检查线程池中线程的周期5分钟-->
    </ConsumeThreadPool>
  </config>

二、对于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文件格式非法!");

共6页  第1页 第2页 第3页 第4页 第5页 第6页

 
 
标签: 线程池 , 代码 , 线程 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站