(
(
(
一
<?xml version=
<config>
<ConsumeThreadPool>
<minPools>
<maxPools>
<checkThreadPeriod>
</ConsumeThreadPool>
</config>
二
import java
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){
this
}
public void setMaxPools(int maxPools){
this
}
public void setCheckThreadPeriod(int checkThreadPeriod){
this
}
public String toString(){
return minPools+
}
public ConsumeThreadPoolPara() {
}
public static void main(String[] args) {
ConsumeThreadPoolPara consumeThreadPool
}
}
三
使用jdom解析
import org
import org
import java
import java
public class ParseConfig {
static Hashtable Listens = null;
static ConnPara connpara = null;
static ConsumeThreadPoolPara consumeThreadPoolPara = null;
private static String configxml =
static{
getConsumeThreadPoolPara(); //得到消費的線程池的參數
}
/**
* 裝載文檔
* @return 返回根結點
* @throws JDOMException
*/
public static Element loadDocument() throws JDOMException{
SAXBuilder parser = new SAXBuilder(); // 新建立構造器
try {
Document document = parser
Element root = document
return root;
}catch(JDOMException e){
logger
throw new JDOMException();
}
}
public static ConsumeThreadPoolPara getConsumeThreadPoolPara(){
if(consumeThreadPoolPara ==null){
try {
Element root = loadDocument();
Element consumeThreadPool = root
if (consumeThreadPool != null) { //代表有數據庫配置
consumeThreadPoolPara = new ConsumeThreadPoolPara();
Element minPools = consumeThreadPool
consumeThreadPoolPara
Element maxPools = consumeThreadPool
consumeThreadPoolPara
Element checkThreadPeriod = consumeThreadPool
consumeThreadPoolPara
}
}
catch (JDOMException e) {
}
}
return consumeThreadPoolPara;
}
}
四
import java
/**
* <p>Title: 線程池</p>
* <p>Description: 采集消費模塊</p>
* <p>Copyright: Copyright (c)
* <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 java
static Object o = new Object();
static{ //先初始化線程池的參數
ConsumeThreadPoolPara consumeThreadPoolPara = ParseConfig
if(consumeThreadPoolPara!=null){
minPools = consumeThreadPoolPara
maxPools = consumeThreadPoolPara
checkThreadPeriod = consumeThreadPoolPara
}
}
public void setMinPools(int minPools){
this
}
public void setMaxPools(int maxPools){
this
}
public void setCheckThreadPeriod(int checkThreadPeriod){
this
}
public ThreadPool() {
m_ThreadList=new ArrayList();
m_RunList=new LinkedList();
for(int i=
WorkerThread temp=new WorkerThread();
totalThread = totalThread +
m_ThreadList
temp
try{
Thread
}catch(Exception e){
}
}
timer = new Timer(true); //啟動定時器
timer
}
/**
* 當有一個工作來的時候啟動線程池的線程
*
*
* @param work
*/
public synchronized void run(String work)
{
if (freeThreadCount ==
if(totalThread<maxPools){
WorkerThread temp = new WorkerThread();
totalThread = totalThread +
m_ThreadList
temp
synchronized(m_RunList){
m_RunList
m_RunList
}
}else{
while (freeThreadCount ==
try {
Thread
}
catch (InterruptedException e) {
}
}
synchronized(m_RunList){
m_RunList
m_RunList
}
}
} else {
synchronized(m_RunList){
m_RunList
m_RunList
}
}
}
/**
* 檢查所有的線程的有效性
*/
public synchronized void checkAllThreads() {
Iterator lThreadIterator = erator();
while (lThreadIterator
WorkerThread lTestThread = (WorkerThread) lThreadIterator
if (! (lTestThread
lTestThread = new WorkerThread(); //重新生成個線程
lTestThread
}
}
}
/**
* 打印調試信息
*/
public void printDebugInfo(){
System
System
}
/**
*
* <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_RunList
try{
m_RunList
if(!running) return;
}catch(InterruptedException e){
}<
From:http://tw.wingwit.com/Article/program/Java/gj/201311/27379.html