熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java高級技術 >> 正文

典型Java線程池的代碼及其各部分功能介紹

2022-06-13   來源: Java高級技術 

  ()根據xml文件來管理線程池的最大最小線程數
  ()對線程池通過Timer定期掃描以防止線程未激活
  ()通過某一個變量(本程序中是freeThreadCount)來得到空閒線程的數目
  
  配置xml(listenxml)是
  <?xml version= encoding=UTF?>
  <config>
  <ConsumeThreadPool>
  <minPools></minPools>   <!線程池最小線程>
  <maxPools></maxPools>    <!線程池最大線程>
  <checkThreadPeriod></checkThreadPeriod> <!檢查線程池中線程的周期分鐘>
  </ConsumeThreadPool>
  </config>
  
  對於ConsumeThreadPoolPara的javabean:
  import javaio*;
  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){
  thisminPools = minPools;
  }
  public void setMaxPools(int maxPools){
  thismaxPools = maxPools;
  }
  public void setCheckThreadPeriod(int checkThreadPeriod){
  thischeckThreadPeriod = checkThreadPeriod;
  }
  public String toString(){
  return minPools+ + maxPools+ +checkThreadPeriod;
  }
  public ConsumeThreadPoolPara() {
  }
  public static void main(String[] args) {
  ConsumeThreadPoolPara consumeThreadPool = new ConsumeThreadPoolPara();
  }
  
  }
  
  解析xml程序代碼(生成ConsumeThreadPoolPara)
  使用jdom解析
  import orgjdom*;
  import orgjdominputSAXBuilder;
  import javaio*;
  import javautil*;
  
  public class ParseConfig {
  static Hashtable Listens = null;
  static ConnPara connpara = null;
  static ConsumeThreadPoolPara consumeThreadPoolPara = null;
  private static String configxml = listenxml;
  
  static{
  getConsumeThreadPoolPara(); //得到消費的線程池的參數
  }
  
  /**
  * 裝載文檔
  * @return 返回根結點
  * @throws JDOMException
  */
  public static Element loadDocument() throws JDOMException{
  SAXBuilder parser = new SAXBuilder(); // 新建立構造器
  try {
  Document document = parserbuild(configxml);
  Element root = documentgetRootElement();
  return root;
  }catch(JDOMException e){
  loggererror(listenxml文件格式非法!);
  throw new JDOMException();
  }
  }
  
  public static ConsumeThreadPoolPara getConsumeThreadPoolPara(){
  if(consumeThreadPoolPara ==null){
  try {
  Element root = loadDocument();
  Element consumeThreadPool = rootgetChild(ConsumeThreadPool);
  if (consumeThreadPool != null) { //代表有數據庫配置
  consumeThreadPoolPara = new ConsumeThreadPoolPara();
  Element minPools = consumeThreadPoolgetChild(minPools);
  consumeThreadPoolParasetMinPools(IntegerparseInt(minPoolsgetTextTrim()));
  Element maxPools = consumeThreadPoolgetChild(maxPools);
  consumeThreadPoolParasetMaxPools(IntegerparseInt(maxPoolsgetTextTrim()));
  Element checkThreadPeriod = consumeThreadPoolgetChild(checkThreadPeriod);
  consumeThreadPoolParasetCheckThreadPeriod(IntegerparseInt(checkThreadPeriodgetTextTrim()));
  }
  }
  catch (JDOMException e) {
  }
  }
  return consumeThreadPoolPara;
  }
  }
  
  線程池源代碼
  import javautil*;
  
  /**
  * <p>Title: 線程池</p>
  * <p>Description: 采集消費模塊</p>
  * <p>Copyright: Copyright (c) </p>
  * <p>Company: </p>
  * @author 張榮斌
  * @version
  */
  
  public class ThreadPool {
  private static int minPools = ; //最小連接池數目
  private static int maxPools = ; //最大連接池數目
  private static int checkThreadPeriod = ; //檢查連接池的周期
  ArrayList m_ThreadList; //工作線程列表
  LinkedList m_RunList = null; //工作任務列表
  int totalThread = ; //總線程數
  static int freeThreadCount = ; //未被使用的線程數目
  private javautilTimer timer = null; //定時器
  static Object o = new Object();
  
  static{ //先初始化線程池的參數
  ConsumeThreadPoolPara consumeThreadPoolPara = ParseConfiggetConsumeThreadPoolPara();
  if(consumeThreadPoolPara!=null){
  minPools = consumeThreadPoolParagetMinPools();
  maxPools = consumeThreadPoolParagetMaxPools();
  checkThreadPeriod = consumeThreadPoolParagetCheckThreadPeriod()**;
  }
  }
  public void setMinPools(int minPools){
  thisminPools = minPools;
  }
  public void setMaxPools(int maxPools){
  thismaxPools = maxPools;
  }
  public void setCheckThreadPeriod(int checkThreadPeriod){
  thischeckThreadPeriod = checkThreadPeriod;
  }
  public ThreadPool() {
  
  m_ThreadList=new ArrayList();
  m_RunList=new LinkedList();
  for(int i=;i<minPools;i++){
  WorkerThread temp=new WorkerThread();
  totalThread = totalThread + ;
  m_ThreadListadd(temp);
  tempstart();
  try{
  Threadsleep();
  }catch(Exception e){
  }
  }
  timer = new Timer(true); //啟動定時器
  timerschedule(new CheckThreadTask(this)checkThreadPeriod);
  }
  
  /**
  * 當有一個工作來的時候啟動線程池的線程
  * 當空閒線程數為的時候看總線程是否小於最大線程池的數目就new一個新的線程否則sleep直到有空閒線程為止;
  * 當空閒線程不為則將任務丟給空閒線程去完成
  * @param work
  */
  public synchronized void run(String work)
  {
  if (freeThreadCount == ) {
  if(totalThread<maxPools){
  WorkerThread temp = new WorkerThread();
  totalThread = totalThread + ;
  m_ThreadListadd(temp);
  tempstart();
  synchronized(m_RunList){
  m_RunListadd(work);
  m_RunListnotify();
  }
  }else{
  while (freeThreadCount == ) {
  try {
  Threadsleep();
  }
  catch (InterruptedException e) {
  }
  }
  synchronized(m_RunList){
  m_RunListadd(work);
  m_RunListnotify();
  }
  }
  } else {
  synchronized(m_RunList){
  m_RunListadd(work);
  m_RunListnotify();
  }
  }
  }
  
  /**
  * 檢查所有的線程的有效性
  */
  public synchronized void checkAllThreads() {
  
  Iterator lThreadIterator = erator();
  
  while (lThreadIteratorhasNext()) { //逐個遍厲
  WorkerThread lTestThread = (WorkerThread) lThreadIteratornext();
  
  if (! (lTestThreadisAlive())) { //如果處在非活動狀態時
  lTestThread = new WorkerThread(); //重新生成個線程
  lTestThreadstart(); //啟動
  }
  }
  }
  
  /**
  * 打印調試信息
  */
  public void printDebugInfo(){
  Systemoutprintln(totalThread=+totalThread);
  Systemoutprintln(m_ThreadListsize()=+m_ThreadListsize());
  }
  
  /**
  *
  * <p>Title: 工作線程類</p>
  * @author 張榮斌
  * @version
  */
  class WorkerThread extends Thread{
  boolean running = true;
  String work;
  
  public void run(){
  while(running){
  synchronized(o){
  freeThreadCount++;
  }
  synchronized(m_RunList){
  while(m_RunListsize() == ){
  try{
  m_RunListwait();
  if(!running) return;
  }catch(InterruptedException e){
  }<
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27379.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.