免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1737 | 回复: 0
打印 上一主题 下一主题

JAVA服务器端Socket线程池 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-07-14 17:51 |只看该作者 |倒序浏览

      
import
java.util.Vector;
  import java
.net
.*;
  import java.io.*;
  public
class
ThreadPool {
  public static final
int
MAX_THREADS = 100;
  public static final int MAX_SPARE_THREADS = 50;
  public static final int MIN_SPARE_THREADS = 10;
  public static final int WORK_WAIT_TIMEOUT = 60 * 1000;
  protected Vector pool; //存放空闲
线程

  protected MonitorRunnable monitor; //A monitor
thread
that monitors the pool for idel threads.
  protected int maxThreads; //Max number of threads that you can open in the pool.
  protected int minSpareThreads; //Min number of idel threads that you can leave in the pool.
  protected int maxSpareThreads; //Max number of idel threads that you can leave in the pool.
  protected int currentThreadCount; //Number of threads in the pool.
  protected int currentThreadsBusy; //Number of busy threads in the pool.
  protected boolean stopThePool; //
Flag
that the pool should terminate all the threads and stop.
  /**
  * Construct
  */
  public ThreadPool() {
  maxThreads = MAX_THREADS;
  maxSpareThreads = MAX_SPARE_THREADS;
  minSpareThreads = MIN_SPARE_THREADS;
  currentThreadCount = 0;
  currentThreadsBusy = 0;
  stopThePool = false;
  }
  /**
  * 启动线程池
  */
  public synchronized void start() {
  adjustLimits(); //调整最大和最小线程数及最大和最小多余线程数.
  openThreads(minSpareThreads); //打开初始线程
  monitor = new MonitorRunnable(this); //Runnable对象实例 //A monitor thread that monitors the pool for idel threads.
  }
  public void setMaxThreads(int maxThreads) {
  this.maxThreads = maxThreads;
  }
  public int getMaxThreads() {
  return maxThreads;
  }
  public void setMinSpareThreads(int minSpareThreads) {
  this.minSpareThreads = minSpareThreads;
  }
  public int getMinSpareThreads() {
  return minSpareThreads;
  }
  public void setMaxSpareThreads(int maxSpareThreads) {
  this.maxSpareThreads = maxSpareThreads;
  }
  public int getMaxSpareThreads() {
  return maxSpareThreads;
  }
  /**
  * 线程池管理方法.
  * 当空闲队列线程中没有空闲线程时,则增加处理(空闲)线程数量.
  * 如果线程数量已达到最大线程数,则新的连接进行等待.
  * 当请求到来,且有空闲线程时调用处理线程进行具体业务处理.
  * @param r ThreadPoolRunnable
  */
  public void runIt(Socket cs) { //r 为
task
//有任务进入时调用
  if (null == cs) {
  throw new NullPointerException();
  }
  if (0 == currentThreadCount || stopThePool) {
  throw new IllegalStateException();
  }
  ControlRunnable c = null; //任务处理实例.
  synchronized (this) {
  if (currentThreadsBusy == currentThreadCount) { //如果工作线程和当前线程数相等,说明没有空闲线程.
  if (currentThreadCount  maxSpareThreads) { //如果空闲的线程数大于多余的最大线程数量.
  int toFree = currentThreadCount - currentThreadsBusy - maxSpareThreads; //得出多余的线程数量
  for (int i = 0; i = maxThreads) { //如果最大多余线程数大于最大线程数.
  maxSpareThreads = maxThreads; //设置最大多余线程数为最大线程数.
  }
  if (maxSpareThreads  maxSpareThreads) { //如果最小多余线程大于最大多余线程数
  minSpareThreads = maxSpareThreads; //设置最小多余线程数为最大多余线程数.
  }
  if (minSpareThreads  maxThreads) {
  toOpen = maxThreads;
  }
  if (0 == currentThreadCount) { //如果当前线程池中的线程数量为0
  pool = new Vector(toOpen); //创建一个有minSpareThreads数量的Vector
  }
  //因第二次增加时对第一次增加的线程不能重复增加.所要从currentThreadCount开始.
  for (int i = currentThreadCount; i
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u3/98439/showart_1996193.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP