线程池主要提高线程的复用,减小线程创建和销毁浪费的时间,合理调度线程执行任务。
特别是任务执行时间较短,但任务量较大,线程池更能显示其优势,下面贴出线程池一个简单的实现方式。
package threadPool;
import java.util.Collections;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
/**
* 线程池类
*
* @author lizh
* @date 2014-1-16上午11:04:54
* @fileName ThreadPool.java
* @package threadPool
* @project Test
*/
public class ThreadPool {
/* 单例 */
private static ThreadPool instance;
/* 已经处理的任务数 */
private static int taskCounter = 0;
private PoolWorker[] poolWorks;
private static List<Task> workQuence = Collections
.synchronizedList(new LinkedList<Task>());
private ThreadPool(int size) {
poolWorks = new PoolWorker[size];
for (int i = 0; i < poolWorks.length; i++) {
poolWorks[i] = new PoolWorker(i);
}
}
public static ThreadPool getInstance() {
if (instance == null)
return new ThreadPool(5);
return instance;
}
public static ThreadPool getInstance(int size) {
if (instance == null)
return new ThreadPool(size);
return instance;
}
public void addTask(Task newTask) {
synchronized (workQuence) {
newTask.setTaskId(++taskCounter);
newTask.setTaskSubmitTime(new Date());
workQuence.add(newTask);
workQuence.notifyAll();
}
}
public int getWorksCount() {
return workQuence.size();
}
public String getInfo() {
StringBuffer sb = new StringBuffer();
sb.append("\nTask Queue Size:" + workQuence.size());
for (int i = 0; i < poolWorks.length; i++) {
sb.append("\nWorker " + i + " is "
+ ((poolWorks[i].isWaiting()) ? "Waiting." : "Running."));
}
return sb.toString();
}
/**
* 单条线程
*
* @author lizh
* @date 2014-1-16上午11:05:08
* @fileName ThreadPool.java
* @package threadPool
* @project Test
*/
private class PoolWorker extends Thread {
private int index = 0;
private boolean isRunning = true;
private boolean isWaiting = true;
public PoolWorker(int index) {
this.index = index;
super.start();
}
public void stopWorker() {
this.isRunning = false;
}
@Override
public void run() {
while (isRunning) {
Task t = null;
synchronized (workQuence) {
while (workQuence.isEmpty()) {
try {
System.out.println("线程池现在为空等待1秒");
workQuence.wait(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t = workQuence.remove(0);
}
if (t != null) {
isWaiting = false;
t.setBeginTime(new Date());
// new Thread(t).start();
t.run();
t.setEndTime(new Date());
isWaiting = true;
t = null;
}
}
}
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public boolean isRunning() {
return isRunning;
}
public void setRunning(boolean isRunning) {
this.isRunning = isRunning;
}
public boolean isWaiting() {
return isWaiting;
}
public void setWaiting(boolean isWaiting) {
this.isWaiting = isWaiting;
}
}
}
package threadPool;
import java.util.Date;
/**
* 任务类
* @author lizh
* @date 2014-1-16上午11:04:15
* @fileName Task.java
* @package threadPool
* @project Test
*/
public abstract class Task implements Runnable{
private Date taskCreateTime;
private Date taskSubmitTime;
private Date beginTime;
private Date endTime;
private long taskId;
public Task(){
this.taskCreateTime = new Date();
}
public Date getTaskCreateTime() {
return taskCreateTime;
}
public void setTaskCreateTime(Date taskCreateTime) {
this.taskCreateTime = taskCreateTime;
}
public Date getTaskSubmitTime() {
return taskSubmitTime;
}
public void setTaskSubmitTime(Date taskSubmitTime) {
this.taskSubmitTime = taskSubmitTime;
}
public Date getBeginTime() {
return beginTime;
}
public void setBeginTime(Date beginTime) {
this.beginTime = beginTime;
}
public Date getEndTime() {
return endTime;
}
public void setEndTime(Date endTime) {
this.endTime = endTime;
}
public long getTaskId() {
return taskId;
}
public void setTaskId(long taskId) {
this.taskId = taskId;
}
}
测试类1 采用线程池 5000个任务 执行20毫秒左右
package threadPool;
public class Test {
/**
* @auth lizh
* @Date 2014-1-14下午5:48:02
* @method main
* @fileName Test
* @package threadPool
* @project Test
* @param args
* @return void
*/
public static void main(String[] args) {
long s = System.currentTimeMillis();
ThreadPool pool = ThreadPool.getInstance(5);
for(int i=0;i<5000;i++){
Task t = new Task() {
@Override
public void run() {
int i=0;
while(i<10){
i++;
}
}
};
pool.addTask(t);}
while(true){
int count = pool.getWorksCount();
if(count<=0){
System.out.println(System.currentTimeMillis()-s);
return ;
}
}
}
}
测试二不采用线程池 花费300毫秒左右
package threadPool;
public class Test1 {
private static int i=0;
static Object o = new Object();
public synchronized static int setI(){
i++;
return i;
}
public static void main(String[] args) {
long a = System.currentTimeMillis();
for(int i=0;i<5000;i++){
Thread t = new Thread(new Runnable() {
@Override
public void run() {
int i=0;
while(i<10){
i++;
}
setI();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t.start();
}
while(true){
if(i==5000){
System.out.println(System.currentTimeMillis()-a);
System.out.println(i);
return;
}
System.out.println(i);
}
}
}
虽然第二种测试有点误差 因为 setI 是同步方法 线程可能会在这个方法堵塞 但是 线程池优势 还是显而易见的