首先声明,代码部分来自网络。
1、入口D*Test:
package com.lbh.myThreadPool; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.lbh.myThreadPool.ThreadPool; public class D*Test { public static void main(String[] args) { //初始化池,并创建3条未执行任务的线程
ThreadPool threadPool = new ThreadPool(3);
threadPool.initPool(); //模拟DTO
List humanList = new ArrayList();
for(int i=0; i<10;i++){
Map human = new HashMap();
human.put("name", "jack"+i);
human.put("age", i+"");
humanList.add(human);
} //模拟BO,遍历任务,并添加到池中
D*Impl bianBian;
for(int i = 0 ; i< humanList.size(); i++){
Map myHuman = (Map)humanList.get(i);
bianBian = new D*Impl(myHuman.get("name").toString(),myHuman.get("age").toString());
threadPool.addJob(bianBian);//向池中添加新任务 try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
2:线程池ThreadPool
package com.lbh.myThreadPool; import java.util.LinkedList; /**
* 线程池,包含调度及其他各种功能
*/
public class ThreadPool { protected int poolSize;//池默认容量
protected LinkedList<D*Thread> threadList = new LinkedList<D*Thread>();
protected boolean hasFreeThread = true;//池状态,true为有空闲线程。 public ThreadPool(int poolSize) {
this.poolSize = poolSize;
} /**
* 初始化池,创建等于池容量的线程数。
*/
public void initPool() {
for (int i = 0; i < poolSize; i++) {
D*Thread d*Thread = new D*Thread(this);
Thread thread = new Thread(d*Thread);
thread.start();
threadList.add(d*Thread);
}
System.out.println("初始化成功_"+"线程池容量:"+poolSize);
} /**
* 添加新任务到空闲线程中
*/
public void addJob(D* D*){
//先拿到空闲线程
D*Thread freeTh = getFreeThread();
if(freeTh != null && freeTh.hasJob == false){
freeTh.hasJob = true;
freeTh.d* = D*;
freeTh.notifyJob();
}
else System.out.println("获取空线程失败,请检查。");
} /**
* 获取空闲线程
*/
public D*Thread getFreeThread(){
while(true){
for(int i=0; i < threadList.size(); i++){
D*Thread D*Th = threadList.get(i);
if(!D*Th.hasJob){
return D*Th;
}
}
System.out.println("线程池已满,请等待。");
hasFreeThread = false;//将池状态设置为false //阻塞,直到有空闲线程
if(waitForFree()){
continue;
}
}
} /**
*更新空闲线程状态
*/
public void updateFreeState(){
this.hasFreeThread = true;
} /**
* 设置阻塞,直到有空闲线程
*/
public boolean waitForFree(){ while(!hasFreeThread){
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
continue;
}
return true;
} }
3、线程处理D*Thread
package com.lbh.myThreadPool; /**
*@comment
*@author LolOT
*@date 2015-3-6 下午4:40:24
*@version 1.0.0
*@see
*/
public class D*Thread implements Runnable { ThreadPool pool;
D* d*;
boolean hasJob; //初始化需要执行的线程对象
D*Thread(ThreadPool pool){
this.pool = pool;
this.hasJob = false;
} //唤醒休眠的线程,用于向池内添加任务时调用
public synchronized void notifyJob() {
hasJob = true;
this.notify();
} @Override
//运行线程,这里判断一下该线程中是否有任务,没有的话就wait。
public synchronized void run() {
try {
while (true) {
if (!hasJob) {
pool.hasFreeThread = true;
System.out.println("当前线程空闲,线程号:" + Thread.currentThread().getId());
this.wait();
} else {
d*.process();
hasJob = false;
}
}
} catch (InterruptedException e) {
e.toString();
}
}
}
4、业务处理实现类D*Impl
package com.lbh.myThreadPool; import java.text.SimpleDateFormat;
import java.util.Date; public class D*Impl implements D* { String humanName;
String humanAge;
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss SS");// 设置日期格式 public D*Impl(String humanName, String humanAge){
this.humanName = humanName;
this.humanAge = humanAge;
} @Override
public void process() {
System.out.println("当前:"+humanName+"开始大便。" + sdf.format(new Date())+"线程号:"+Thread.currentThread().getId());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println(humanName+"大便完成。" + sdf.format(new Date())+"线程号:"+Thread.currentThread().getId());
}
}
5、业务处理接口D*
/**
*
*/
package com.lbh.myThreadPool; public interface D* { public void process();
}