java的线程池的使用
1、线程池的创建
1、首先创建一个类,然后实现Runnable接口
public class ExectorTest implements Runnable {}
2、首先声明一个线程池的全局变量
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; }
3、然后在run()方法中,创建线程池实例,创建线程池的时候,切记创建守护线程,这样可以防止你的服务停止后,服务的线程还没停止,就是tomcat的进程还在的情况出现。
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void run() { // 创建线程池,设置为守护进程,可以和主线程一起关闭 this.executorPool = Executors.newFixedThreadPool(this.numThreads, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); } }
4、然后我们需要循环需要处理的方法个数,在循环中调用线程池的方法
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void run() { // 创建线程池,设置为守护进程,可以和主线程一起关闭 this.executorPool = Executors.newFixedThreadPool(this.numThreads, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } }
5、将处理的方法贴上
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void run() { // 创建线程池,设置为守护进程,可以和主线程一起关闭 this.executorPool = Executors.newFixedThreadPool(this.numThreads, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } /** * 数据处理线程 */ public static class Handler implements Runnable { public Handler() {} @Override public void run() { //处理数据的方法 } } }
2、线程池的关闭
6、最后补全停止线程池的方法,@PreDestroy方法是在spring销毁之前会调用的方法
public class ExectorTest implements Runnable { //线程池 private ExecutorService executorPool; @Override public void run() { // 创建线程池,设置为守护进程,可以和主线程一起关闭 this.executorPool = Executors.newFixedThreadPool(this.numThreads, new ThreadFactory() { @Override public Thread newThread(Runnable r) { Thread thread = Executors.defaultThreadFactory().newThread(r); thread.setDaemon(true); return thread; } }); //循环处理的方法 List<String> handleList = new ArrayList<String>(); for (String handler:handleList) { this.executorPool.submit(new Handler()); } } /** * 数据处理线程 */ public static class Handler implements Runnable { public Handler() {} @Override public void run() { //处理数据的方法 } } @PreDestroy public void shutdown() { // 关闭线程池,会等待线程的执行完成 if (this.executorPool != null) { // 关闭线程池 this.executorPool.shutdown(); // 等待关闭完成, 等待五秒 try { if (!this.executorPool.awaitTermination(5, TimeUnit.SECONDS)) { log.info("Timed out waiting for consumer threads to shut down, exiting uncleanly!!"); } } catch (InterruptedException e) { log.info("Interrupted during shutdown, exiting uncleanly!!"); } } } }
相关文章