工作中用到的线程池代码,单例模式写的,感觉不错.记下来.
public class ThreadPool{
//公用线程
public static final String commonThreads="commonThreads";
//公用线程个数
//TODO 可以配置
public static final int commonThreadCount=2;
//所有池列表
private static List<ThreadPool> pools=new LinkedList<ThreadPool>();
private static ThreadPool pool=null;
/**
* 线程池的唯一编号
*/
private String poolId="default Thread pool thread";
//----------------------
/**
* 任务队列
*/
private TaskQueue taskQueue=new TaskQueue();
/**
* 线程池中所有线程列表
*/
private List<PoolThread> threads=new LinkedList<PoolThread>();
private ThreadPool(int threadCount,String id) {
this.poolId=id;
this.init(threadCount);
}
private ThreadPool(int threadCount) {
this.init(threadCount);
}
private void init(int threadCount){
for(int i=0;i<threadCount;i++){
PoolThread poolThread=new PoolThread(this.taskQueue);
threads.add(poolThread);
poolThread.setDaemon(true);
poolThread.setName(poolId);
poolThread.start();
}
}
/**
* 添加一项任务到线程池
* @param runnable
*/
public void addTask(Runnable task){
this.taskQueue.addTask(task);
}
/**
* 获得指定ID的线程池对象,如果没创建,就创建,threadCount为对应线程池的线程个数
* @param poolId
* @param threadCount
* @return
*/
public static ThreadPool getInstance(String poolId,int threadCount){
ThreadPool pool=getPool(poolId);
if(pool==null){
pool=new ThreadPool(threadCount,poolId);
pools.add(pool);
}
return pool;
}
/**
* 返回指定ID的线程组,如果没有就返回空
* @param poolId
* @return
*/
public static ThreadPool getInstance(String poolId){
ThreadPool pool=getPool(poolId);
return pool;
}
/**
* 获得进行公共事务处理的线程
* @return
*/
public static ThreadPool getInstance(){
return getInstance(commonThreads, commonThreadCount);
}
/**
* 返回指定ID的线程池
* @param id
* @return
*/
private static ThreadPool getPool(String id){
ThreadPool ret=null;
int len=pools.size();
for(int i=0;i<len;i++){
ThreadPool pool=pools.get(i);
if(pool.getId().equals(id)){
ret=pool;
return ret;
}
}
return ret;
}
public String getId(){
return this.poolId;
}
}
class TaskQueue{
//任务链表
private List<Runnable> tasks=new LinkedList<Runnable>();
/**
* 添加一任务到队列
* @param task
*/
public synchronized void addTask(Runnable task) {
this.tasks.add(task);
if(this.getTaskCount()<=1){//有线程被挂起
// System.out.println("叫醒");
this.notify();
}
}
/**
* 当前线程将被挂起,直到有可用的任务到达
* @return
*/
public synchronized Runnable getTask(){
Runnable ret=null;
while(true){
if(this.getTaskCount()<=0){
try {
// System.out.println("wait......");
this.wait();
} catch (InterruptedException e) {}
}else{
ret=this.tasks.remove(0);
break;
}
}
return ret;
}
public synchronized int getTaskCount(){
return this.tasks.size();
}
}
/**
* 线程池中的线程
*
*
*/
class PoolThread extends Thread{
private TaskQueue queue;
PoolThread(TaskQueue queue){
this.queue=queue;
}
public void run(){
while(true){
Runnable run=queue.getTask();
run.run();
//HelperMethods.sleepThread();//让出处理器
}
}
}
}
}
分享到:
相关推荐
java开发过程,经常会用到线程池技术,这里简单介绍一个构建线程池的实例
corePoolSize:核心池的大小,在创建了线程池后,线程池中的线程数为0,当有任务来之后,就会创建一个线程去执行任务,当线程池中的线程数目达到corePoolSize后,就会把到达的任务放到缓存队列当中; ...
讲述了java线程池的优点,参数,6种线程池的使用场景,线程池用到的handler,线程任务的提交方式等等。
多并发时用到的线程池类,C++所写
从易语言转型到C#感觉C#自带的线程池不够灵活(或许是自己的技术不够),所以自己写了一个线程池工作类为了配合自己的使用习惯,有兴趣的可以下载参考 代码用到的是信号处理以及队列等一些比较基础的东西,已经测试了功能...
qt编写tcp服务器程序,用到线程池。在vs2010/qtcreator4.8下运行正常.
线程池是什么 QThreadPool 线程池是一种线程使用模式,它管理着...它的主要目的是减少程序员编写的重复代码,提高程序的效率和性能,在高并发的项目中会用到,比如node.js有很多异步操作,底层就是用线程池来处理的。
里面用到了线程池,作为服务器的首选,开发者可以从中学到很多东西
在Linux 系统下面用C 语言实现的高并发服务器的代码,具体用到了Epoll,线程池,数据库连接池
最近做工程用到了这些知识,因此总结一些这方面的资料,我认为还是比较有用的,通过这些资料,大概一天就可以学会噢~
在Linux 系统下面用C 语言实现的高并发服务器的代码,具体用到了Epoll,线程池,数据库连接池。 具体可以看下连接http://blog.csdn.net/wuyuxing24/article/details/48758927
最近阿里发布的 Java开发手册中强制线程池不允许使用 Executors 去创建,而是通过 ThreadPoolExecutor 的方式,这样的处理方式让写的同学更加明确线程池的运行规则,规避资源耗尽的风险。 其中ThreadFactoryBuilder...
,向线程池中投递一个可执行的函数子程序指针,和投递任务_int()基本一模一样,在内部自动转换成指针地址到整数(子程序指针) 6. 投递任务_int() 向线程池中投递一个可执行的函数指针地址 7. 等待任务动态() :就是...
线程池和mysql连接池的实现,可以在ubuntu12.04下完美实现,mysql连接池需要用到mysql-connector c++
android listview异步加载图片实例 用到了线程池 下载的图片会保存到本地 并在数据库中保留记录 再次加载时会直接从本地读取
实现的高并发服务器的代码,使用的是C语编写的,用到了Epoll,线程池,数据库连接池。
java锁和线程池,这都是一类常见的问题和分析,做java开发肯定会用到,当然可能也有不对的地方,欢迎指正
2)由于用到线程池,可以进行多任务网络请求操作 3)没有网络的时候进行网络状态检查 4)对请求的数据根据URL进行缓存到本地sqlite数据库中(我刚刚想起了 post请求的时候需要对缓存url及本地数据存取做修改才可以...
多个文件同时下载多线程下载,用到线程池优化线程,发广播更新下载进度,数据库保存下载进度
*修正 修正鱼刺类_线程池Ex/线程池Ex一处很难被触发的BUG。*修改 去除所有用到取启动时间的函数/方法 (修正在系统开机超过29天的机器上可能会出现问题)。*修正 类回调_生成 部分系统配置兼容问题。5.4.2(2017-11-21)...