已阅读5页,还剩5页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
Java实现通用线程池 线程池通俗的描述就是预先创建若干空闲线程,等到需要用多线程去处理事务的时候去唤醒某些空闲线程执行处理任务,这样就省去了频繁创建线程的时间,因为频 繁创建线程是要耗费大量的CPU资源的。如果一个应用程序需要频繁地处理大量并发事务,不断的创建销毁线程往往会大大地降低系统的效率,这时候线程池就派 上用场了。 本文旨在使用Java语言编写一个通用的线程池。当需要使用线程池处理事务时,只需按照指定规范封装好事务处理对象,然后用已有的线程池对象去自动选择空 闲线程自动调用事务处理对象即可。并实现线程池的动态修改(修改当前线程数,最大线程数等)。下面是实现代码:/ThreadTask .java package polarman.threadpool; /* * 线程任务 * author ryang * 2006-8-8 */ public interface ThreadTask public void run(); /PooledThread.java package polarman.threadpool; import java.util.Collection; import java.util.Vector; /* * 接受线程池管理的线程 * author ryang * 2006-8-8 */ public class PooledThread extends Thread protected Vector tasks = new Vector(); protected boolean running = false; protected boolean stopped = false; protected boolean paused = false; protected boolean killed = false; private ThreadPool pool; public PooledThread(ThreadPool pool) this.pool = pool; public void putTask(ThreadTask task) tasks.add(task); public void putTasks(ThreadTask tasks) for(int i=0; i 0) return (ThreadTask)tasks.remove(0); else return null; public boolean isRunning() return running; public void stopTasks() stopped = true; public void stopTasksSync() stopTasks(); while(isRunning() try sleep(5); catch (InterruptedException e) public void pauseTasks() paused = true; public void pauseTasksSync() pauseTasks(); while(isRunning() try sleep(5); catch (InterruptedException e) public void kill() if(!running) interrupt(); else killed = true; public void killSync() kill(); while(isAlive() try sleep(5); catch (InterruptedException e) public synchronized void startTasks() running = true; this.notify(); public synchronized void run() try while(true) if(!running | tasks.size() = 0) pool.notifyForIdleThread(); /System.out.println(Thread.currentThread().getId() + : 空闲); this.wait(); else ThreadTask 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() + : Tasks are paused); break; running = false; if(killed) killed = false; break; catch(InterruptedException e) return; /System.out.println(Thread.currentThread().getId() + : Killed); /ThreadPool.java package polarman.threadpool; import java.util.Collection; import java.util.Iterator; import java.util.Vector; /* * 线程池 * author ryang * 2006-8-8 */ public class ThreadPool protected int maxPoolSize; protected int initPoolSize; protected Vector threads = new Vector(); protected boolean initialized = false; protected boolean hasIdleThread = false; public ThreadPool(int maxPoolSize, int initPoolSize) this.maxPoolSize = maxPoolSize; this.initPoolSize = initPoolSize; public void init() initialized = true; for(int i=0; iinitPoolSize; i+) PooledThread thread = new PooledThread(this); thread.start(); threads.add(thread); /System.out.println(线程池初始化结束,线程数= + threads.size() + 最大线程数= + maxPoolSize); public void setMaxPoolSize(int maxPoolSize) /System.out.println(重设最大线程数,最大线程数= + maxPoolSize); this.maxPoolSize = maxPoolSize; if(maxPoolSize getPoolSize() for(int i=getPoolSize(); isize & imaxPoolSize; i+) PooledThread thread = new PooledThread(this); thread.start(); threads.add(thread); else if(size size) PooledThread th = (PooledThread)threads.remove(0); th.kill(); /System.out.println(重设线程数,线程数= + threads.size(); public int getPoolSize() return threads.size(); protected void notifyForIdleThread() hasIdleThread = true; protected boolean waitForIdleThread() hasIdleThread = false; while(!hasIdleThread & getPoolSize() = maxPoolSize) try Thread.sleep(5); catch (InterruptedException e) return false; return true; public synchronized PooledThread getIdleThread() while(true) for(Iterator itr=threads.iterator(); itr.hasNext();) PooledThread th = (PooledThread)itr.next(); if(!th.isRunning() return th; if(getPoolSize() = 2) try int size = Integer.parseInt(words1); pool.setPoolSize(size); catch(Exception e) else if(words0.equalsIgnoreCase(max) & words.length = 2) try int max = Integer.parseInt(words1); pool.setMaxPoolSize(max); catch(Exception e) else if(words0.equalsIgnoreCase(task) & words.length = 3) try int timelen = Integer.parseInt(words2); SimpleTask task = new SimpleTask(words1, timelen * 1000); cessTask(task); catch(Exception e) catch (IOException e) e.printStackTrace(); ; cmdThread.start(); /* for(int i=0; i10; i+) SimpleTask task = new SimpleTask(Task + i, (i+10)*1000); cessTask(task); */ class SimpleTask implements ThreadTask private String taskName; private int timeLen; public SimpleTask(String taskName, int timeLen) this.taskName = taskName; this.timeLen = timeLen; public void run() System.out.println(Thread.currentThread().getId() + : START TASK + taskName + ); try Thread.sleep(timeLen); catch (InterruptedException e) System.out.println
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年健康生活与心理健康知识竞赛试卷
- 2026年志愿者服务社会调查能力测试题
- 2026年苏教版小学语文三年级下册第12单元看图写话专项练习
- 2026年网络安全意识培养专项训练习题
- 2026年四川省人教版初中化学下册第4章化学实验习题集
- Unit 2 Helping at home第3课时 Fuel up 教学评教学设计英语外研版四年级上册2026秋
- 第三单元 第3课时 节余多少钱(分层作业)数学北师大版三年级上册2026秋
- 探寻精神动物:测试题及答案
- 土方施工现场管理题目及答案
- 博士语文考试试题及答案
- 兵检职业适应测试题及答案
- 2025年游戏代练兼职合同模板
- 2025年中级咖啡师资格考试试题与答案
- 外包单位安全管理制度
- 民用建筑设计术语标准
- 医疗护理员课件
- 护理阿尔兹海默病
- 学习护理知识和急救常识
- 人教PEP版英语五年级下册Unit 1~6全册教案共6个单元整体教学设计
- GB/T 19183.1-2024电气和电子设备机械结构户外机壳第1部分:设计导则
- DL∕T 318-2017 输变电工程施工机具产品型号编制方法
评论
0/150
提交评论