快速业务通道

jBPM-4.0中文开发指南-第4章 架构 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-14

Java服务器端Socket线程池

时间:2011-09-24

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 < maxThreads) { //如果当前线程数还没有达到最大线程数. int toOpen = currentThreadCount + minSpareThreads; //再增加minSpareThreads个线程量. openThreads(toOpen); //打开线程新增空闲线程. //currentThreadCount数量增加 } else { //如果当前数量达到了最大线程数. wh

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号