首先声明,代码部分来自网络。
1、入口DabianTest:
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 DabianTest { 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,遍历任务,并添加到池中 DabianImpl bianBian; for(int i = 0 ; i< humanList.size(); i++){ Map myHuman = (Map)humanList.get(i); bianBian = new DabianImpl(myHuman.get("name").toString(),myHuman.get("age").toString()); threadPool.addJob(bianBian);//向池中添加新任务 try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }
2:线程池ThreadPool
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lbh.myThreadPool; import java.util.LinkedList; /** * 线程池,包含调度及其他各种功能 */ public class ThreadPool { protected int poolSize;//池默认容量 protected LinkedList<DabianThread> threadList = new LinkedList<DabianThread>(); protected boolean hasFreeThread = true;//池状态,true为有空闲线程。 public ThreadPool(int poolSize) { this.poolSize = poolSize; } /** * 初始化池,创建等于池容量的线程数。 */ public void initPool() { for (int i = 0; i < poolSize; i++) { DabianThread dabianThread = new DabianThread(this); Thread thread = new Thread(dabianThread); thread.start(); threadList.add(dabianThread); } System.out.println("初始化成功_"+"线程池容量:"+poolSize); } /** * 添加新任务到空闲线程中 */ public void addJob(Dabian Dabian){ //先拿到空闲线程 DabianThread freeTh = getFreeThread(); if(freeTh != null && freeTh.hasJob == false){ freeTh.hasJob = true; freeTh.dabian = Dabian; freeTh.notifyJob(); } else System.out.println("获取空线程失败,请检查。"); } /** * 获取空闲线程 */ public DabianThread getFreeThread(){ while(true){ for(int i=0; i < threadList.size(); i++){ DabianThread DabianTh = threadList.get(i); if(!DabianTh.hasJob){ return DabianTh; } } 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、线程处理DabianThread
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lbh.myThreadPool; /** *@comment *@author LolOT *@date 2015-3-6 下午4:40:24 *@version 1.0.0 *@see */ public class DabianThread implements Runnable { ThreadPool pool; Dabian dabian; boolean hasJob; //初始化需要执行的线程对象 DabianThread(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 { dabian.process(); hasJob = false; } } } catch (InterruptedException e) { e.toString(); } } }
4、业务处理实现类DabianImpl
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.lbh.myThreadPool; import java.text.SimpleDateFormat; import java.util.Date; public class DabianImpl implements Dabian { String humanName; String humanAge; SimpleDateFormat sdf = new SimpleDateFormat("HH:mm:ss SS");// 设置日期格式 public DabianImpl(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、业务处理接口Dabian
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/** * */ package com.lbh.myThreadPool; public interface Dabian { public void process(); }