下午在调试多线程时发现,虽然确认每次都是将线程池关闭java
executor.shutdown(); executor.awaitTermination(1, TimeUnit.SECONDS)
可是第二个调用任务进来线程池序号仍是增长了,没找到缘由。当时想了两种解决方法,第一种是用spring来管理线程池,第二种是使用单列模式。看了下网上关于使用spring管理线程池的代码,考虑到我这个功能并无这么重量级,须要改配置文件来处理。简单处理,使用单列模式作。spring
1.建立自定义名称的线程池多线程
package com.demo.thread.test20170811; import java.util.concurrent.BlockingQueue; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; /** * 自定义名称线程池 * @author fsw * */ public class NamedThreadPoolExecutor extends ThreadPoolExecutor{ private String name; public NamedThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler) { super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
2.建立单列模式ide
package com.demo.thread.test20170811; import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ExecutorService; import java.util.concurrent.RejectedExecutionHandler; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.ReentrantLock; /** * 线程池管理类 * @author fsw * */ public class SingletonThreadPool { final NamedThreadPoolExecutor executor = new NamedThreadPoolExecutor(5,5,0,TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(5), new RejectedExecutionHandler() { @Override public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) { NamedThreadPoolExecutor namedThreadPoolExe = (NamedThreadPoolExecutor) executor; try { System.out.println(namedThreadPoolExe.getName()); executor.getQueue().put(r); } catch (Exception e) { } } }); private static SingletonThreadPool threadPool = null; private static ReentrantLock lock = new ReentrantLock(); public static SingletonThreadPool getInstance() { try { lock.lock(); if(threadPool == null) { threadPool = new SingletonThreadPool(); return threadPool; } } finally { lock.unlock(); } return threadPool; } public ExecutorService getExecutorService(String name) { executor.setName(name); return executor; } public Long getComTaskCount() { return executor.getCompletedTaskCount(); } }
3.建立执行任务类测试
package com.demo.thread.test20170811; import java.util.concurrent.Callable; public class Task implements Callable<Void>{ @Override public Void call() throws Exception { System.out.println(Thread.currentThread().getName()); return null; } }
4.测试类this
package com.demo.thread.test20170811; import java.util.concurrent.CompletionService; import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutorCompletionService; public class SingletonThreadMain { public static void main(String[] args) throws InterruptedException, ExecutionException { final SingletonThreadMain m = new SingletonThreadMain(); m.runMethod("a"); m.runMethod("b"); m.runMethod("c"); } public void runMethod(final String name) throws InterruptedException, ExecutionException { SingletonThreadPool threadPool = SingletonThreadPool.getInstance(); CompletionService<Void> completionService = new ExecutorCompletionService<Void>( threadPool.getExecutorService(name)); for (int i = 0; i < 100; i++) { completionService.submit(new Task()); } for (int i = 0; i < 100; i++) { completionService.take().get(); } System.out.println("==================="+SingletonThreadPool.getInstance().getComTaskCount()); } }
5.执行结果线程
从执行结果可见,线程池序号一直都是1。问题初步解决,后面遇到问题再更新调试