前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java创建线程池代码_java手写线程池

java创建线程池代码_java手写线程池

作者头像
全栈程序员站长
发布2022-11-14 14:55:22
7560
发布2022-11-14 14:55:22
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

jdk提供了一个通过ThreadPoolExecutor创建一个线程池的类

构造器

使用给定的参数和默认的饱和策略、默认的工厂方法创建线程池

代码语言:javascript
复制
ThreadPoolExecutor(int corePoolSize, 
int maximumPoolSize, 
long keepAliveTime, 
TimeUnit unit, 
BlockingQueue<Runnable> workQueue)

使用给定的参数和默认的工厂方法创建线程池

代码语言:javascript
复制
ThreadPoolExecutor(int corePoolSize, 
int maximumPoolSize, 
long keepAliveTime, 
TimeUnit unit, 
BlockingQueue<Runnable> workQueue, 
RejectedExecutionHandler handler)

使用给定的参数和默认的饱和策略(AbortPolicy)创建线程池

代码语言:javascript
复制
ThreadPoolExecutor(int corePoolSize, 
int maximumPoolSize,
long keepAliveTime,
BlockingQueue<Runnable> workQueue, 
ThreadFactory threadFactory)

使用指定的参数创建线程池

代码语言:javascript
复制
ThreadPoolExecutor(int corePoolSize, 
int maximumPoolSize, 
long keepAliveTime, 
TimeUnit unit,
BlockingQueue<Runnable> workQueue, 
RejectedExecutionHandler handler)

参数说明

  • corePoolSize 线程池的基本大小, 当提交一个任务到线程池的时候,线程池会创建一个线程来执行任务,即使当前线程池已经存在空闲线程,仍然会创建一个线程,等到需要执行的任务数大于线程池基本大小时就不再创建。如果调用线程池的prestartAllCoreThreads()方法,线程池会提前创建并启动所有的基本线程。
  • maximumPoolSizeSize 线程池最大数量,线程池允许创建的最大线程数,如果队列满了,并且已创建的线程数小于最大线程数,则线程池会再创建新的线程执行任务。值得注意的是,如果使用了无界的任务队列这个参数就没什么效果。
  • keepAliveTime 线程活动保持时间,线程池的工作线程空闲后,保持存活的时间,所以,如果任务很多,并且每个任务执行的时间比较短,可以调大时间,提高线程的利用率。
  • unit 线程活动保持时间的单位,可选择的单位有时分秒等等。
  • workQueue 任务队列。用来暂时保存任务的工作队列
  • threadFactory 用于创建线程的工厂

队列

  • ArrayBlockingQueue:是一个基于数组结构的有界阻塞队列,此队列按照FIFO(先进先出)原则对元素进行排序
  • DelayQueue
  • LinkedBlockingDeque
  • LinkedBlockingQueue:是一个基于链表结构的有界阻塞队列,此队列按照FIFO排序元素,吞吐量高于ArrayBlockingQueue。静态工厂方法Executors.newFixedThreadPool(n)使用了此队列
  • LinkedTransferQueue
  • PriorityBlockingQueue:一个具有优先级的无限阻塞队列
  • SynchronousQueue:一个不存储元素的阻塞队列。每个插入操作必须等待另一个线程调用移除操作,否则插入操作一直处于阻塞状态,吞吐量要高于LinkedBlockingQueue,静态工厂方法Executors.newCachedThreadPool()使用了此队列

饱和策略

当队列和线程池都满了,说明线程池处于饱和的状态,那么必须采取一种策略处理提交的新任务。这个策略默认是AbortPolicy,表示无法处理新任务时抛出异常

  • ThreadPoolExecutor.AbortPolicy:直接抛出异常
  • ThreadPoolExecutor.CallerRunsPolicy:只用调用这所在的线程来运行任务
  • ThreadPoolExecutor.DiscardOldestPolicy:丢弃队列里最近的一个任务,并执行当前任务
  • ThreadPoolExecutor.DiscardPolicy:不处理,丢弃掉

示例

代码语言:javascript
复制
public class ThreadPool { 

/** * 线程池的基本大小 */
static int corePoolSize = 10;
/** * 线程池最大数量 */
static int maximumPoolSizeSize = 100;
/** * 线程活动保持时间 */
static long keepAliveTime = 1;
/** * 任务队列 */
static ArrayBlockingQueue workQueue = new ArrayBlockingQueue(10);
public static void main(String[] args) { 

ThreadPoolExecutor executor = new ThreadPoolExecutor(
corePoolSize,
maximumPoolSizeSize,
keepAliveTime,
TimeUnit.SECONDS,
workQueue,
new ThreadFactoryBuilder().setNameFormat("XX-task-%d").build());
//提交一个任务
executor.execute(() -> System.out.println("ok"));
}
}

源码分析

任务执行

代码语言:javascript
复制
public void execute(Runnable command) { 

if (command == null)
throw new NullPointerException();
/* * Proceed in 3 steps: * * 1. If fewer than corePoolSize threads are running, try to * start a new thread with the given command as its first * task. The call to addWorker atomically checks runState and * workerCount, and so prevents false alarms that would add * threads when it shouldn't, by returning false. * * 2. If a task can be successfully queued, then we still need * to double-check whether we should have added a thread * (because existing ones died since last checking) or that * the pool shut down since entry into this method. So we * recheck state and if necessary roll back the enqueuing if * stopped, or start a new thread if there are none. * * 3. If we cannot queue task, then we try to add a new * thread. If it fails, we know we are shut down or saturated * and so reject the task. */
int c = ctl.get();
if (workerCountOf(c) < corePoolSize) { 

if (addWorker(command, true))
return;
c = ctl.get();
}
if (isRunning(c) && workQueue.offer(command)) { 

int recheck = ctl.get();
if (! isRunning(recheck) && remove(command))
reject(command);
else if (workerCountOf(recheck) == 0)
addWorker(null, false);
}
else if (!addWorker(command, false))
reject(command);
}

参考文档

https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadPoolExecutor.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/187339.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年9月30日 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 构造器
  • 队列
  • 饱和策略
  • 示例
  • 源码分析
  • 参考文档
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档