⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threadmanager.java

📁 采用JAVA开发
💻 JAVA
字号:
package com.gctech.sms.util;

import EDU.oswego.cs.dl.util.concurrent.PooledExecutor;
import EDU.oswego.cs.dl.util.concurrent.BoundedBuffer;
import org.apache.log4j.Logger;
import java.util.Properties;
import java.io.*;
import EDU.oswego.cs.dl.util.concurrent.Executor;

/**
 * <p>Title: 线程管理者。</p>
 * <p>Description: 线程管理者。</p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: gctech</p>
 * @author 王红宝
 * @version $Id: ThreadManager.java,v 1.1.1.1 2004/04/21 09:30:41 wanghb Exp $
 */

public class ThreadManager {
  static final String CONFIG_PREFIX = "gctech.thread.";
  int boundSize = 100;
  public void shutdownNow(){
    pool.shutdownNow();
    pool = null;
  }
  public String getThreadPoolInfo(){
    StringBuffer sb = new StringBuffer();

    sb.append("活动线程总数:").append(Thread.activeCount()).append("\n\r")
        .append("线程池信息:\n\r")
        .append("线程池当前大小:").append(pool.getPoolSize()).append("\n\r")
        .append("线程池最大值:").append(pool.getMaximumPoolSize()).append("\n\r")
        .append("线程池最小值:").append(pool.getMinimumPoolSize()).append("\n\r")
        .append("队列大小:").append(buffer.capacity()).append("\n\r")
        .append("队列当前使用大小:").append(buffer.size()).append("\n\r");
    return sb.toString();
  }

  //线程池
  PooledExecutor pool;
  public void loadThreadPool(Properties props){
    /* Using a bounded buffer of 100 tasks,
     at least 4 threads (started only when needed due to incoming requests),
     but allowing up to 100 threads if the buffer gets full.
     pre-start 9 threads, allowing them to die if they are not used for
     five minutes. clients block if both the buffer is full and
     all 100 threads are busy*/
    int maxSize = 100;
    int minSize = 10;
    int keepAlive = 1000*60*5;
    int initSize = 15;

    boundSize = Integer.parseInt(props.getProperty(CONFIG_PREFIX+"BoundSize"));
    maxSize = Integer.parseInt(props.getProperty(CONFIG_PREFIX+"MaxPoolSize"));
    minSize = Integer.parseInt(props.getProperty(CONFIG_PREFIX+"MinPoolSize"));
    keepAlive = Integer.parseInt(props.getProperty(CONFIG_PREFIX+"KeepAliveTime"));
    initSize = Integer.parseInt(props.getProperty(CONFIG_PREFIX+"InitSize"));
    buffer = new BoundedBuffer(boundSize);
    pool = new PooledExecutor(buffer, maxSize);
    pool.setMinimumPoolSize(minSize);
    pool.setKeepAliveTime(keepAlive);
    pool.waitWhenBlocked();
    pool.createThreads(initSize);
    logger.info("Thread pool load OK!");
  }
  public void loadThreadPool(String cfg){

    Properties props = new Properties();
    InputStream in = null;
    try {
      in = new FileInputStream(cfg);
      props.load(in);
      in.close();
      loadThreadPool(props);
    }catch (IOException ex) {
      logger.warn("load thread config from "+cfg+" failed!");
    }
  }
  //启动线程池,多个ConcurrentServer可以共享线程池
  public void loadThreadPool(){
    loadThreadPool("./conf/threadp.ini");
  }
  BoundedBuffer buffer;
  private ThreadManager() {
    //loadThreadPool();
  }
  private static ThreadManager singleton;
  public static ThreadManager getInstance(){
    if ( singleton == null )
      singleton = new ThreadManager();
    return singleton;
  }
  public Executor getExcecutor(){
    return pool;
  }
  public void execute(Runnable command) throws InterruptedException {
    pool.execute(command);
  }
  static final Logger logger = Logger.getLogger(ThreadManager.class);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -