java

位置:IT落伍者 >> java >> 浏览文章

典型Java线程池的代码及其各部分功能介绍


发布日期:2022年01月27日
 
典型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){

}<               

上一篇:Java设计模式:责任链

下一篇:用Java Annotations管理对象生命周期