UtilProperties.java
package
org.ofbiz.smsSend;

import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.util.Properties;

public
class
UtilProperties

...
{
private String fileName = "config.properties";
private Properties prop;
private FileInputStream in;
private FileOutputStream out;
public UtilProperties(String filePath) throws IOException

...{
this.fileName = filePath;
getFile();
}
public UtilProperties()

...{
try

...{
getFile();
}
catch (IOException e)

...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void getFile() throws IOException

...{
File file = new File(this.fileName);
in = new FileInputStream(file);
prop = new Properties();
prop.load(in);
in.close();
}
public void list()

...{
prop.list(System.out);
}
public String getValue(String itemName)

...{
return prop.getProperty(itemName);
}
public void setValue(String itemName,String value)

...{
prop.setProperty(itemName, value);
}
public void saveFile() throws IOException

...{
File f = new File(this.fileName);
out = new FileOutputStream(f);
prop.store(out, "");
out.close();
}
public void deleteValue(String value)

...{
prop.remove(value);
}
public void changeFile(String filePath)

...{
this.fileName = filePath;
try

...{
getFile();
}
catch (IOException e)

...{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) throws IOException

...{
UtilProperties up = new UtilProperties();
up.list();
up.changeFile("config.properties");
up.list();
System.out.println(" " + up.getValue("temSavePath"));
}
}
package
org.ofbiz.smsSend;

import
java.util.ArrayList;
import
java.util.Iterator;
import
java.util.LinkedList;
import
java.util.Timer;

public
class
ThreadPool

...
{
private static final UtilProperties utilProp = new UtilProperties();

private static int minPools = Integer.parseInt(utilProp
.getValue("minPools"));

private static int maxPools = Integer.parseInt(utilProp
.getValue("maxPools"));

private static int checkThreadPeriod = Integer.parseInt(utilProp
.getValue("checkThreadPeriod")) * 60 * 1000;

private static ArrayList workThreadList; // 工作线程列表,保存所有的线程

private static LinkedList taskList = null; // 工作任务列表,保存将要执行的工作任务

private static int totalThread = 0; // 总线程数

private static int freeThreadCount = 0; // 未被使用的线程数目

private java.util.Timer timer = null; // 定时器

private static Object o = new Object();

private static ThreadPool pool = new ThreadPool();

public static void setMinPools(int minPools)

...{
ThreadPool.minPools = minPools;
}

public static void setMaxPools(int maxPools)

...{
ThreadPool.maxPools = maxPools;
}

public static void setCheckThreadPeriod(int checkThreadPeriod)

...{
ThreadPool.checkThreadPeriod = checkThreadPeriod;
}

private ThreadPool()

...{
workThreadList = new ArrayList();
taskList = new LinkedList();
// 初始化线程池
for (int i = 0; i < ThreadPool.minPools; i++)

...{
WorkerThread temp = new WorkerThread();
totalThread = totalThread + 1;
workThreadList.add(temp);
temp.start();
try

...{
Thread.sleep(100);
}
catch (Exception e)

...{
}
}
timer = new Timer(true); // 启动定时器
timer.schedule(new CheckThreadTask(this), 0, checkThreadPeriod);
}

public static ThreadPool getInstance()

...{
return pool;
}

public synchronized void run(Work work)

...{
if (freeThreadCount == 0)

...{
if (totalThread < maxPools)

...{
WorkerThread temp = new WorkerThread();
totalThread = totalThread + 1;
workThreadList.add(temp);
temp.start();
synchronized (taskList)

...{
taskList.add(work);
taskList.notify();
}
}
else

...{
while (freeThreadCount == 0)

...{
try

...{
Thread.sleep(200);
}
catch (InterruptedException e)

...{
}
}
synchronized (taskList)

...{
taskList.add(work);
taskList.notify();
}
}
}
else

...{
synchronized (taskList)

...{
taskList.add(work);
taskList.notify();
}
}
}


/** *//** */

/** *//**
* 检查工作线程列表,将非活动状态的线程换成活动状态的线程,保证线程池中的线程可用 119 * 120
*/
public synchronized void checkAllThreads()

...{

Iterator threadIterator = workThreadList.iterator();

while (threadIterator.hasNext())

...{ // 逐个遍厉
WorkerThread workThread = (WorkerThread) threadIterator.next();

if (!(workThread.isAlive()))

...{
// 如果处在非活动状态时
workThread = new WorkerThread(); // 重新生成1个线程
workThread.start(); // 启动
}
}
}

public static void printInfo()

...{
System.out.println("minPools:" + minPools);
System.out.println("maxPools:" + maxPools);
System.out.println("checkThreadPeriod:" + checkThreadPeriod);
System.out.println("totalThread=" + totalThread);
System.out.println("workThreadList.size()=" + workThreadList.size());
}


/** *//** */

/** *//**
* 149 * 线程池中的工作线程类,由工作线程执行我们要进行的操作 150
*/
class WorkerThread extends Thread

...{
boolean running = true;

Work work;

public void run()

...{
while (running)

...{
synchronized (o)

...{
freeThreadCount++; // 一进来说明多了一个可用线程
}
synchronized (taskList)

...{
while (taskList.size() == 0) // 当工作任务列表为空时,等待

...{
try

...{
taskList.wait();
if (!running)
return;
}
catch (InterruptedException e)

...{
}
}
synchronized (o)

...{
freeThreadCount--; // 得到一个工作,可用线程要减1
}
work = (Work) taskList.removeLast(); // 从任务列表处获得一个任务
if (work == null)
return;
}
work.doWork();
}
}
}
}
package
org.ofbiz.smsSend;

import
java.util.
*
;

public
class
CheckThreadTask
extends
TimerTask

...
{
private static boolean isRunning = false;
private ThreadPool pool;
public CheckThreadTask(ThreadPool pool)

...{
this.pool = pool;
}
@Override
public void run()

...{
// TODO Auto-generated method stub
if(!isRunning)

...{
isRunning = true;
pool.checkAllThreads();
isRunning = false;
}
}

}
package
org.ofbiz.smsSend;

public
interface
Work

...
{
public abstract void doWork();
}
package
org.ofbiz.smsSend;

public
class
Test
implements
Work

...
{


/** *//**
* @param args
*/
public static void main(String[] args)

...{
// TODO Auto-generated method stub
ThreadPool.getInstance().run(new Test());
ThreadPool.printInfo();
}

public void doWork()

...{
// TODO Auto-generated method stub
System.out.println("a");
}

}











































































































ThreadPool.java












































































































































































































































CheckThreadTask.java

































Work.java








Test.java


























