java写的简单通用线程池demo
2024-09-13 03:43:26
首先声明,代码部分来自网络。
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
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
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
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
/**
*
*/
package com.lbh.myThreadPool; public interface Dabian { public void process();
}
最新文章
- 两个Canvas小游戏
- jQuery插件的编写和使用 <;思维导图>;
- 转Java 回调函数的理解
- 洛谷 P1169 [ZJOI2007]棋盘制作
- UVA 11175 From D to E and Back
- UVA 1001 Say Cheese
- C++和JNI的数据转换
- Banner图二三事
- Java(6)for循环
- ShellSort
- 从注册表清理 IE10,IE11 用户代理字符串(UserAgent)中的垃圾信息
- 从GitHub远程仓库中删除文件夹或文件
- 最小化安装centos5.5
- 3DLut表实现log视频的后期调色原理
- 【docker】docker部署spring boot服务,但是docker logs查看容器输出控制台日志,没有日志打印,日志未打印,docker logs不打印容器日志
- react-router v4 使用 history 控制路由跳转
- jenkins 自动化部署php
- PyQt5--QFontDiaglog
- SQL记录-PLSQL条件控制
- mysql 创建和删除用户
热门文章
- 各Android版本的Linux内核表(待续)以及如何下载相应的android源码
- ContentProvider官方教程(6)provider支持的数据类型
- Python3基础 type获取变量的类型
- 在JSP页面中输出完整的时间
- MySQL(三) —— 约束以及修改数据表
- tophat cufflinks cuffcompare cuffmerge 的使用
- Metasploit基础命令
- FZU 2221 RunningMan(跑男)
- bzoj 1202: [HNOI2005]狡猾的商人 并查集好题
- CSS笔记(四)文本