java-线程池序号一直增长

原文链接: https://my.oschina.net/u/3445128/blog/1506810

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

下午在调试多线程时发现,虽然确认每次都是将线程池关闭

executor.shutdown();
executor.awaitTermination(1, TimeUnit.SECONDS)

但是第二个调用任务进来线程池序号还是增加了,没找到原因。当时想了两种解决方法,第一种是用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 workQueue, RejectedExecutionHandler handler) {
		super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, handler);
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	
}

2.创建单列模式

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(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{

	@Override
	public Void call() throws Exception {
		System.out.println(Thread.currentThread().getName());
		return null;
	}

}

4.测试类

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 completionService = new ExecutorCompletionService(
				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.执行结果

java-线程池序号一直增长_第1张图片

从执行结果可见,线程池序号一直都是1。问题初步解决,后面遇到问题再更新

转载于:https://my.oschina.net/u/3445128/blog/1506810

你可能感兴趣的:(java-线程池序号一直增长)