




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java中通用的线程池实例代码java中通用的线程池实例代码,需要的朋友可以参考一下代码如下:package com.smart.frame.task.autoTask;import java.util.Collection;import java.util.Vector;/* 任务分发器*/public class TaskManage extends Threadprotected Vector tasks = new Vector();protected boolean running = false;protected boolean stopped = false;protected
2、 boolean paused = false;protected boolean killed = false;private ThreadPool pool;public TaskManage(ThreadPool pool)this.pool = pool;public void putTask(Runnable task)tasks.add(task);public void putTasks(Runnable tasks)for (int i = 0; i tasks.length; i+)this.tasks.add(tasksi);public void putTasks(Col
3、lection tasks)this.tasks.addAll(tasks);protected Runnable popTask()if (tasks.size() 0) return (Runnable) tasks.remove(0);else return null;public boolean isRunning()return running;public void stopTasks()stopped = true;public void stopTasksSync()stopTasks();while (isRunning()trysleep(5)
4、;catch (InterruptedException e)TaskException.getResultMessage(e);public void pauseTasks()paused = true;public void pauseTasksSync()pauseTasks();while (isRunning()trysleep(5);catch (InterruptedException e)TaskException.getResultMessage(e);public void kill()if (!running) interrupt();else killed = true
5、;public void killSync()kill();while (isAlive()trysleep(5);catch (InterruptedException e)TaskException.getResultMessage(e);public synchronized void startTasks()running = true;this.notify();public synchronized void run()trywhile (true)if (!running | tasks.size() = 0)pool.notifyForIdleThread();this.wai
6、t(); elseRunnable task;while (task = popTask() != null)task.run();if (stopped)stopped = false;if (tasks.size() 0)tasks.clear();System.out.println(Thread.currentThread().getId() + : Tasks are stopped);break;if (paused)paused = false;if (tasks.size() 0)System.out.println(Thread.currentThread().getId()
7、 + : Tasks are paused);break;running = false;if (killed)killed = false;break;catch (InterruptedException e)TaskException.getResultMessage(e);return;复制代码 代码如下:package .frame.task.autoTask;import java.util.Collection;import java.util.Iterator;import java.util.Vector;/* 线程池*/
8、public class ThreadPoolprotected int maxPoolSize = TaskConfig.maxPoolSize;protected int initPoolSize = TaskConfig.initPoolSize;protected Vector threads = new Vector();protected boolean initialized = false;protected boolean hasIdleThread = false;public ThreadPool()super();public ThreadPool(int maxPoo
9、lSize, int initPoolSize)this.maxPoolSize = maxPoolSize;this.initPoolSize = initPoolSize;public void init()initialized = true;for (int i = 0; i initPoolSize; i+)TaskManage thread = new TaskManage(this);thread.start();threads.add(thread);public void setMaxPoolSize(int maxPoolSize)this.maxPoolSize = ma
10、xPoolSize;if (maxPoolSize getPoolSize()for (int i = getPoolSize(); i size & i maxPoolSize; i+)TaskManage thread = new TaskManage(this);thread.start();threads.add(thread); else if (size size)TaskManage th = (TaskManage) threads.remove(0);th.kill();public int getPoolSize()return threads.size();protect
11、ed void notifyForIdleThread()hasIdleThread = true;protected boolean waitForIdleThread()hasIdleThread = false;while (!hasIdleThread & getPoolSize() = maxPoolSize)tryThread.sleep(5);catch (InterruptedException e)TaskException.getResultMessage(e);return false;return true;public synchronized TaskManage
12、getIdleThread()while (true)for (Iterator itr = threads.iterator(); itr.hasNext();)TaskManage th = (TaskManage) itr.next();if (!th.isRunning() return th;if (getPoolSize() maxPoolSize)TaskManage thread = new TaskManage(this);thread.start();threads.add(thread);return thread;if (waitForIdleThread() = fa
13、lse) return null;public void processTask(Runnable task)TaskManage th = getIdleThread();if (th != null)th.putTask(task);th.startTasks();public void processTasksInSingleThread(Runnable tasks)TaskManage th = getIdleThread();if (th != null)th.putTasks(tasks);th.startTasks();public void processTasksInSin
14、gleThread(Collection tasks)TaskManage th = getIdleThread();if (th != null)th.putTasks(tasks);th.startTasks();复制代码 代码如下:package com.smart.frame.task.autoTask;public class TopTask implements Runnableprivate ThreadPool pool;public TopTask()super();public TopTask(ThreadPool pool)super();this.pool = pool;Overridepublic void run()init();start();/* 初始化验证权限、参数之类*/publi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2024年记者证考试的评估体系介绍试题及答案
- 2024年秘书证考试主观题解析试题及答案
- 海鲜安全管理与试题及答案
- 二级建造师考试合同法律基础试题及答案
- 档案保护知识测试题及答案
- 心内科门诊日常管理
- 收纳师考试备考试题及答案指南
- 二级建造师考试题型试题及答案
- 力矩平衡实验设计试题及答案
- 光彩夺目的珠宝鉴定师考试试题答案
- 第四单元第十课第二框题 保护人身权 同步练习(无答案)2024-2025学年七年级下册道德与法治
- 2025年中国铁路青藏集团限公司公开招聘635人笔试自考难、易点模拟试卷(共500题附带答案详解)
- 浙江2025年桐乡市事业单位招考高频重点模拟试卷提升(共500题附带答案详解)
- 药械不良事件知识培训
- 2025至2031年中国管道直饮水系统行业投资前景及策略咨询研究报告
- 现场材料进场验收培训
- 外卖骑手交通安全培训
- 【指导规则】央企控股上市公司ESG专项报告参考指标体系
- 电力公司安全生产责任制度
- 工程机械租赁服务方案及保障措施 (二)
- 《矩阵式组织结构》课件
评论
0/150
提交评论