线程池-解析xml.doc_第1页
线程池-解析xml.doc_第2页
线程池-解析xml.doc_第3页
线程池-解析xml.doc_第4页
线程池-解析xml.doc_第5页
已阅读5页,还剩2页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

(1)根据xml文件来管理线程池的最大最小线程数(2)对线程池通过Timer定期扫描以防止线程未激活;(3)通过某一个变量(本程序中是freeThreadCount)来得到空闲线程的数目;一、配置xml(listen.xml)是:101005二、对于ConsumeThreadPoolPara的javabean: import java.io.*;public class ConsumeThreadPoolPara implements Serializableprivate int minPools;private int maxPools;private int checkThreadPeriod;public int getMinPools()return minPools;public int getMaxPools()return maxPools;public int getCheckThreadPeriod() 网管联盟www.bitsCN.comreturn 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; 网管联盟www.bitsCN.com private static String configxml = listen.xml;staticgetConsumeThreadPoolPara();/得到消费的线程池的参数/* 装载文档* return 返回根结点* throws JDOMException*/public static Element loadDocument() throws JDOMExceptionSAXBuilder 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); 中国网管联盟www、bitsCN、comconsumeThreadPoolPara.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; /检查连接池的周期 中国网管联盟www、bitsCN、comArrayList 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(); 网管联盟www.bitsCN.com m_RunList=new LinkedList();for(int i=0;iminPools;i+)WorkerThread temp=new WorkerThread();totalThread = totalThread + 1;m_ThreadList.add(temp);temp.start();tryThread.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(totalThreadmaxPools)WorkerThread temp = new WorkerThread();totalThread = totalThread + 1;m_ThreadList.add(temp);temp.start();synchronized(m_RunList)m_RunList.add(work);m_RunList.notify(); 中国网管联盟www_bitscn_com elsewhile (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); 中国网管联盟www_bitscn_comSystem.out.println(m_ThreadList.size()=+m_ThreadList.size();/* Title: 工作线程类* author 张荣斌* versi

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论