java线程池模式--来自work thread模式的优化
来源:互联网 发布:枕头什么牌子好 知乎 编辑:程序博客网 时间:2024/05/21 05:58
在一篇文章中:工作线程模式,未考虑如下情况:
1.任务里很大时,工作线程不足时,导致客户端提交任务将阻塞的情况
2.任务里很少时,应该回收池中空闲的线程数量,回收资源
1.线程池类:
package threadpool;import java.util.LinkedList;import java.util.Vector;/** * 负责接收和存储任务,以及工作线程生命周期,主要职责如下: * 1.submit接口,提供给客户端 ,客户端 通过此接口提交任务 * 到线程池 * * 2.shutdowm接口,关闭本池并释放池中资源 * @author lxb * */public class ThreadPool {private final LinkedList<TaskRequest> requestQueue; // 保存的请求队列private int count; // 请求队列中的请求数量private final LinkedList<WorkerThread> threadPool; // 工人线程池public ThreadPool(int threads) {this.requestQueue = new LinkedList<TaskRequest>();this.count = 0;threadPool = new LinkedList<WorkerThread>();for (int i = 0; i < threads; i++) { threadPool.addLast(new WorkerThread("Worker-" + i, this));}}/** * 当任务量很大时,添加工作线程 * @param thread */public void addWorkers(WorkerThread thread){threadPool.addLast(thread);thread.start();}/** * 当工作量很少时删除工作线程,节省开支 * @param count */public void deleteWorkers(int count){if(count > threadPool.size()){//这里不取=保证池中始终有一个线程在工作for(int i=0;i<count;i++){threadPool.pop();}}}/** * 开启工作线程 */public void startWorkers() {for (int i = 0; i < threadPool.size(); i++) {threadPool.get(i).start();}}public void stopAllWorkers(){for(int i=0;i<threadPool.size();i++){threadPool.get(i).stopThread();}}/** * 保存客户端发来的工作请求 * * @param request * @throws InterruptedException */public synchronized void putRequest(TaskRequest request) throws InterruptedException {// 当Request的数量大于或等于同时接受的数目时候,要等待while (!requestQueue.isEmpty()&&count >= requestQueue.size() ) {wait();}requestQueue.addLast(request);count++;notifyAll();}public synchronized TaskRequest takeRequest() throws InterruptedException {while (count <= 0) {wait();}TaskRequest request = requestQueue.pop();count--;notifyAll();return request;}}
package threadpool;/** * 工作线程,处理客户端发来的具体的工作请求 * @author lxb * */public class WorkerThread extends Thread{private final ThreadPool threadPool;private volatile boolean terminated = false;//停止请求标志public WorkerThread(String name,ThreadPool threadPool){super(name);this.threadPool = threadPool;}public void run() { try { while (!terminated) { try { TaskRequest request = threadPool.takeRequest(); request.execute(); } catch (InterruptedException e) { terminated = true; } } } finally { System.out.println(Thread.currentThread().getName() + " is terminated."); } } public void stopThread() { terminated = true; interrupt(); }}
3.工作请求类:
package threadpool;import java.util.Random;/** * 任务请求实体 * @author lxb * */public class TaskRequest {private final String name; private final int number; private static final Random random = new Random(); public TaskRequest(String name, int number) { this.name = name; this.number = number; } public void execute() { System.out.println(Thread.currentThread().getName() + " executes " + this); try { Thread.sleep(random.nextInt(1000)); } catch (InterruptedException e) { } } public String toString() { return "[ Request from " + name + " No." + number + " ]"; }}
package threadpool;import java.util.Random;/** * 不停的发送请求给工人类 * * 1.创建请求实例,并将实例发送给管理工人线程的类 * * @author lxb * */public class ClientThread extends Thread {private final ThreadPool channel;private static final Random random = new Random();private volatile boolean terminated = false; // 停止请求标志public ClientThread(String name, ThreadPool channel) {super(name);this.channel = channel;}public void run() {try {for (int i = 0; !terminated; i++) {try {TaskRequest request = new TaskRequest(getName(), i);channel.putRequest(request);Thread.sleep(random.nextInt(1000));} catch (InterruptedException e) {terminated = true;}}} finally {System.out.println(Thread.currentThread().getName()+ " is terminated.");}}public void stopThread() {terminated = true;interrupt();}}
6.测试类:
package threadpool;public class Main {public static void main(String[] args) {// TODO Auto-generated method stub ThreadPool channel = new ThreadPool(5); // Worker Thread数量 channel.startWorkers(); ClientThread alice = new ClientThread("Alice", channel); ClientThread bobby = new ClientThread("Bobby", channel); ClientThread chris = new ClientThread("Chris", channel); alice.start(); bobby.start(); chris.start(); try { Thread.sleep(5000); } catch (InterruptedException e) { } alice.stopThread(); bobby.stopThread(); chris.stopThread(); channel.stopAllWorkers();}}
0 0
- java线程池模式--来自work thread模式的优化
- java多线程设计模式Worker Thread(线程池)
- 线程池模式(Thread Pool)
- Java 多线程编程设计模式之 Thread Pool(线程池)
- miniGUI的“服务”线程(maybe thread模式)
- MySQL为什么要引入Thread Pool的线程处理模式
- java 线程的设计模式
- java设计模式之线程池模式
- Handler+Thread+Message模式 Android线程网络
- Thread Pool 线程池 使用 线程优化
- 多线程设计模式——Thread Pool(线程池)模式
- 设计模式--收藏来自[java温馨小屋]
- 【RabbitMQ】work模式
- Java线程创建模式
- java线程模式
- java单例模式和线程安全的单列模式
- java单例模式的线程安全问题
- java实现线程安全的单利模式
- Android中的Context详解
- 翻转链表
- caffe 安装问题
- ionic2 提示内容
- 文章标题
- java线程池模式--来自work thread模式的优化
- 仿QQ空间点赞动画
- 菜鸟第一次写博客
- maven 加入json-lib.jar 报错 Missing artifact net.sf.json-lib:json-lib:jar:2.4:compile
- nio
- HttpServletRequest和ServletRequest
- 特征选择和特征提取区别 、PCA VS LDA
- Q96:过程纹理(Procedural Texture)(0)——概述
- Linux 零拷贝技术