




已阅读5页,还剩2页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
java服务器端socket线程池importjava.util.vector;.*;importjava.io.*;publicclassthreadpoolpublicstaticfinalintmax_threads=100;publicstaticfinalintmax_spare_threads=50;publicstaticfinalintmin_spare_threads=10;publicstaticfinalintwork_wait_timeout=60*1000;protectedvectorpool;/存放空闲线程protectedmonitorrunnablemonitor;/atectedintmaxthreads;/mtectedintminsparethreads;/mtectedintmaxsparethreads;/mtectedintcurrentthreadcount;/ntectedintcurrentthreadsbusy;/ntectedbooleanstopthepool;/flagthatthepoolshouldterminateallthethreadsandstop./*construct*/publicthreadpool()maxthreads=max_threads;maxsparethreads=max_spare_threads;minsparethreads=min_spare_threads;currentthreadcount=0;currentthreadsbusy=0;stopthepool=false;/*启动线程池*/publicsynchronizedvoidstart()adjustlimits();/调整最大和最小线程数及最大和最小多余线程数.openthreads(minsparethreads);/打开初始线程monitor=newmonitorrunnable(this);/runnable对象实例/amonitorthreadthatmonitorsthepoolforidelthreads.publicvoidsetmaxthreads(intmaxthreads)this.maxthreads=maxthreads;publicintgetmaxthreads()returnmaxthreads;publicvoidsetminsparethreads(intminsparethreads)this.minsparethreads=minsparethreads;publicintgetminsparethreads()returnminsparethreads;publicvoidsetmaxsparethreads(intmaxsparethreads)this.maxsparethreads=maxsparethreads;publicintgetmaxsparethreads()returnmaxsparethreads;/*线程池管理方法.*当空闲队列线程中没有空闲线程时,则增加处理(空闲)线程数量.*如果线程数量已达到最大线程数,则新的连接进行等待.*当请求到来,且有空闲线程时调用处理线程进行具体业务处理.*paramrthreadpoolrunnable*/publicvoidrunit(socketcs)/r为task/有任务进入时调用if(null=cs)thrownewnullpointerexception();if(0=currentthreadcount|stopthepool)thrownewillegalstateexception();controlrunnablec=null;/任务处理实例.synchronized(this)if(currentthreadsbusy=currentthreadcount)/如果工作线程和当前线程数相等,说明没有空闲线程.if(currentthreadcountmaxthreads)/如果当前线程数还没有达到最大线程数.inttoopen=currentthreadcount+minsparethreads;/再增加minsparethreads个线程量.openthreads(toopen);/打开线程新增空闲线程./currentthreadcount数量增加else/如果当前数量达到了最大线程数.while(currentthreadsbusy=currentthreadcount)/当工作线程和当前线程数相等,说明没有空闲线程.trythis.wait();/连接线程进行等待.catch(interruptedexceptione)if(0=currentthreadcount|stopthepool)thrownewillegalstateexception();c=(controlrunnable)pool.lastelement();/在有空闲线程的情况下,从空闲线程队列中取出最后一个线程.pool.removeelement(c);/从空闲队列中删除最后一个线程,用于处理其他事件.currentthreadsbusy+;/对处理事件的线程数加1system.out.println(系统调用一个sokcet线程);c.runit(cs);/调用具体业务方法,告诉其有数据请求要处理,唤醒等待中的线程./*关闭线程池*/publicsynchronizedvoidshutdown()if(!stopthepool)/如果线程池没有关闭,(线程池关闭标识为假)stopthepool=true;monitor.terminate();/关闭监视线程monitor=null;for(inti=0;imaxsparethreads)/如果空闲的线程数大于多余的最大线程数量.inttofree=currentthreadcount-currentthreadsbusy-maxsparethreads;/得出多余的线程数量for(inti=0;itofree;i+)/关闭删除空闲线程,从vector中删除controlrunnablec=(controlrunnable)pool.firstelement();pool.removeelement(c);c.terminate();/让删除的线程结束currentthreadcount-;/处理线程队列减少一个/*当线程处理完成后重新放到空闲线程队列中.*paramccontrolrunnable*/protectedsynchronizedvoidreturncontroller(controlrunnablec)if(0=currentthreadcount|stopthepool)/如果线程池关闭或当前连接线程数量为0c.terminate();/关闭当前线程.return;currentthreadsbusy-;/处理线程队列的数量减少一个pool.addelement(c);/空闲线程队列中增加一个notifyall();/唤醒可能在等待连接的线程./*当一个处理线程出现异常时,要重新开启一个空闭线程.,并唤醒在等待空闲线程的线程.threadpool的runit中等待的线程.*/protectedsynchronizedvoidnotifythreadend()currentthreadsbusy-;/因从线程是在处理数据时出现异常,所处理线程队列的数量要减一个.currentthreadcount-;/因出现异常的线程关闭了.所开户线程的数量要减少一个.notifyall();/唤醒等待连接的阻塞线程.openthreads(minsparethreads);/重新打开minsparethreads个线程.如currentthreadcount的数量大于minsparethreads,则还是不开启新线程./*调整各种线程队列数量*/protectedvoidadjustlimits()if(maxthreads=maxthreads)/如果最大多余线程数大于最大线程数.maxsparethreads=maxthreads;/设置最大多余线程数为最大线程数.if(maxsparethreadsmaxsparethreads)/如果最小多余线程大于最大多余线程数minsparethreads=maxsparethreads;/设置最小多余线程数为最大多余线程数.if(minsparethreadsmaxthreads)toopen=maxthreads;if(0=currentthreadcount)/如果当前线程池中的线程数量为0pool=newvector(toopen);/创建一个有minsparethreads数量的vector/因第二次增加时对第一次增加的线程不能重复增加.所要从currentthreadcount开始.for(inti=currentthreadcount;itoopen;i+)/先增加minsparethreads数量的线程.pool.addelement(newcontrolrunnable(this);/runnable实例对象,可用于创建线程currentthreadcount=toopen;/*监视线程,用于监听当前空闲线程是否大于最大多余线程数量,如存在则关闭多余的空闲线程.*/classmonitorrunnableimplementsrunnablethreadpoolp;threadt;booleanshouldterminate;/*construct*parampthreadpool*/monitorrunnable(threadpoolp)shouldterminate=false;this.p=p;t=newthread(this);t.start();publicvoidrun()while(true)trysynchronized(this)thi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 《劝学》教学设计及获奖说课稿范文
- 化学工业电子化管理系统规程
- 心理压力管理与调适报告
- 小学美术课程标准与教学实施方案
- Web服务监测执行细则
- 化学工业环境管理规划
- 商务装款式潮流总结
- 船舶货物运输备货操作规范
- 农业种植结构调整与优化策略研究
- 学校体育活动规范制度建立
- GB/T 6113.201-2008无线电骚扰和抗扰度测量设备和测量方法规范第2-1部分:无线电骚扰和抗扰度测量方法传导骚扰测量
- GB/T 23101.3-2010外科植入物羟基磷灰石第3部分:结晶度和相纯度的化学分析和表征
- 会诊-联络精神病学-课件
- 玻璃幕墙施工方案完整版
- 部编人教版三年级道德与法治上册全册课件
- 城市道路工程质量事故
- 七律长征教学实录王崧舟3篇
- 铁路路基大维修规则
- Q∕GDW 12178-2021 三相智能物联电能表技术规范
- 合同法中英文对照版
- 小学道法小学道法六年级上-5.国家机构有哪些(第二课时-国家机关的职权)ppt课件
评论
0/150
提交评论