java-线程池序号一直增长

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

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<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.创建单列模式

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.测试类

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。问题初步解决,后面遇到问题再更新

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值