Java之并发 - 创建线程池的方式

一、通过Executors类提供的静态方法创建

1. newFixedThreadPool

public static ExecutorService newFixedThreadPool(int nThreads) {
	return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
}

public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {
	return new ThreadPoolExecutor(nThreads, nThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);
}
  • 生成一个固定大小的线程池,当有新任务提交而没有空闲线程时,将任务置于工作队列,该队列是一个无界队列
  • 当线程池中的一个线程因执行失败而被关闭后,会重新创建一个新的线程
  • 线程池中的线程会一直存活直到线程池被关闭。

2. newSingleThreadExecutor

public static ExecutorService newSingleThreadExecutor() {
	return new FinalizableDelegatedExecutorService
		(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()));
}

public static ExecutorService newSingleThreadExecutor(ThreadFactory threadFactory) {
	return new FinalizableDelegatedExecutorService
		(new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory));
}
  • 在该线程池中自始至终只有一个线程,当有新任务提交而该线程不空闲时,将新任务置于任务队列,队列是一个无界队列
  • 当该线程因执行任务失败而被关闭会会重新创建一个线程
  • 该线程池保证任务按序执行
  • 与newFixedThreadPool的区别是,该线程池后期不能够修改配置即线程池的大小,而nexFixedThreadPool可以

3. newCachedThreadPool

public static ExecutorService newCachedThreadPool() {
	return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>());
}

public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
	return new ThreadPoolExecutor(0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory);
}
  • 仅创建所需数目的线程,当有新任务提交时,从线程池中取出一个空闲线程执行,若没有空闲线程则创建一个新线程
  • 该线程池不会限制线程池的大小,可以创建JVM允许的最大创建数
  • 当线程的空闲时间超过60秒时,该线程会被关闭,减少资源的消耗

4. newScheduledThreadPool

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) {
	return new ScheduledThreadPoolExecutor(corePoolSize);
}

public static ScheduledExecutorService newScheduledThreadPool(int corePoolSize, ThreadFactory threadFactory) {
	return new ScheduledThreadPoolExecutor(corePoolSize, threadFactory);
}
  • 创建一个定长的线程池,并支持定时及周期性执行任务
    

二、通过ThreadPoolExecutor的构造方法

public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, ThreadFactory threadFactory, RejectedExecutionHandler handler) {
	if (corePoolSize < 0 ||
		maximumPoolSize <= 0 ||
		maximumPoolSize < corePoolSize ||
		keepAliveTime < 0)
		throw new IllegalArgumentException();
	if (workQueue == null || threadFactory == null || handler == null)
		throw new NullPointerException();
	this.acc = System.getSecurityManager() == null ?
			null :
			AccessController.getContext();
	this.corePoolSize = corePoolSize;
	this.maximumPoolSize = maximumPoolSize;
	this.workQueue = workQueue;
	this.keepAliveTime = unit.toNanos(keepAliveTime);
	this.threadFactory = threadFactory;
	this.handler = handler;
}
  • corePoolSize:核心线程数,即使没有任务处理,核心线程也会一直存活。若当前线程数小于核心线程数时,即使有空闲线程,也会创建一个新的线程执行任务。
  • maximumPoolSize:最大线程数,即线程池允许创建的最大线程数。
  • keepAliveTime:空闲线程的保活时间,当线程空闲时间达到该值时,该线程会退出,直至线程数减至核心线程数。
  • unit:keepAliveTime的单位。
  • workQueue:工作队列,存放待提交的任务。
  • threadFactory:创建线程的工厂。
  • handler:当线程池已满而又有任务提交时采用的策略,具体Java 线程池的拒绝策略 RejectedExecutorHandler
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值