java多线程线程管理
话不多说,上代码
package com.net.web.core.task; import com.net.web.core.task.job.Hook; import com.net.web.core.task.job.JobThread; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; /** * Created by Administrator on 2017-11-03. */ public class ThreadManager<E extends JobThread> { // 最大线程数 private int maxThread = 0; // 线程队列 List<JobThread> jobs = new ArrayList<JobThread>(); // 线程池 private ScheduledExecutorService executor; public ThreadManager(){ } /** * 实例化 * * @param maxThread 最大线程数量 * @param jobs 要执行的任务队列 */ public ThreadManager(int maxThread, List<JobThread> jobs) { this.maxThread = maxThread; this.jobs = jobs; } /** * 执行并等待执行完毕 */ public void waitStart() { waitStart(null); } /** * 执行并等待执行完毕,有回调 */ public void waitStart(Hook hook) { startThread(hook); // 结束 executor.shutdown(); // 是否结束 while (!executor.isTerminated()) { try { Thread.sleep(1000); } catch (InterruptedException e) { } } // 调用全部结束 if (hook != null) hook.allEnd(); } /** * 执行,但是不等待执行完毕 */ public void start(final Hook hook) { // 开启线程 startThread(hook); new Thread() { @Override public void run() { // 结束 executor.shutdown(); // 是否结束 while (!executor.isTerminated()) { try { Thread.sleep(1000); } catch (InterruptedException e) { } } // 调用全部结束 if (hook != null) hook.allEnd(); } }.start(); } /** * 初始化 */ private void startThread(Hook hook) { // 没有线程 if (maxThread == 0) { throw new NullPointerException("MaxThread is null."); } // 线程池初始化 executor = Executors.newScheduledThreadPool(maxThread); // 最大数量 int allNum = jobs.size(); if (hook != null) hook.setAllNum(allNum); // 线程队列 for (int i = 0; i < allNum; i++) { // 创建线程 JobThread jobThread = jobs.get(i); jobThread.setHook(hook); jobThread.setNum(i+1); // 执行 executor.execute(jobThread); } } public int getMaxThread() { return maxThread; } public void setMaxThread(int maxThread) { this.maxThread = maxThread; } public List<JobThread> getJobs() { return jobs; } public void setJobs(List<JobThread> jobs) { this.jobs = jobs; } }
钩子类,方便回馈
package com.net.web.core.task.job; /** * 进度管理,钩子 * * Created by Administrator on 2017-11-03. */ public class Hook{ // 所有数量 private int allNum; /** * 一个进程开始运行的时候 * @param index */ public void start(int index){}; /** * 一个进程结束的时候 * * @param index */ public void end(int index){}; /** * 一个进程异常的时候 * * @param index */ public void error(int index){}; /** * 所有进程结束的时候 * */ public void allEnd(){}; public int getAllNum() { return allNum; } public void setAllNum(int allNum) { this.allNum = allNum; } }
线程类
package com.net.web.core.task.job; /** * Created by Administrator on 2017-11-03. */ public abstract class JobThread implements Runnable { // 线程编号 protected int num; // 钩子 protected Hook hook; @Override public void run(){ try { // 开始钩子 if(hook != null) hook.start(this.num); // 定时任务 renTask(); // 结束钩子 if(hook != null) hook.end(this.num); }catch (Throwable e){ if(hook != null) hook.error(this.num); } } public abstract void renTask() throws Throwable; public int getNum() { return num; } public void setNum(int num) { this.num = num; } public Hook getHook() { return hook; } public void setHook(Hook hook) { this.hook = hook; } }