Java如何手动创建线程池

这篇文章主要介绍了Java如何手动创建线程池,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

如何手动创建线程池

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

构造器

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

ThreadPoolExecutor(int corePoolSize,  int maximumPoolSize,  long keepAliveTime,  TimeUnit unit,  BlockingQueue workQueue)

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

ThreadPoolExecutor(int corePoolSize,  int maximumPoolSize,  long keepAliveTime,  TimeUnit unit,  BlockingQueue workQueue,  RejectedExecutionHandler handler)

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

ThreadPoolExecutor(int corePoolSize,  int maximumPoolSize, long keepAliveTime, BlockingQueue workQueue,  ThreadFactory threadFactory)

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

ThreadPoolExecutor(int corePoolSize,  int maximumPoolSize,  long keepAliveTime,  TimeUnit unit, BlockingQueue 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:不处理,丢弃掉

示例

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"));     } }

源码分析

任务执行

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) 

参考文档

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

线程池工具类

实现线程的三种方式

1.继承 Thread 类

2.实现Runnable 接口

3.实现 Callbale接口和Future接口实现

4.三种方式比较:

继承Thread 类 编程简单,可扩展性差。

实现接口方式 可扩展性高,编程复杂。

使用ThreadPoolExecutor编写线程池工具类

1.线程创建方式,实例化贤臣池时,创建核心线程,

2.当任务大于核心线程时将进入阻塞队列

3.当阻塞队列满时,任务没有超过最大线程时创建新的线程

4.当任务 > 最大线程数+阻塞队列 时,执行拒绝策略。 

public class ThreadPoolUtils { public static ThreadPoolExecutor pool=null; // 无响应执行 public static void execute(Runnable runnable){ getThreadPool().execute(runnable); } // 有响应执行 public static Future submit(Callable callable){ return getThreadPool().submit(callable); } // 创造线程池 private static synchronized ThreadPoolExecutor getThreadPool(){ if(pool==null){ // 获取处理器数量 int cpuNum = Runtime.getRuntime().availableProcessors(); // 根据cpu数量,计算出合理的线程并发数 // 最佳线程数目 = ((线程等待时间+线程CPU时间)/线程CPU时间 )* CPU数目 int maximumPoolSize = cpuNum * 2 + 1; // 七个参数 // 1. 核心线程数 // 2. 最大线程数 // 3. 空闲线程最大存活时间 // 4. 时间单位 // 5. 阻塞队列 // 6. 创建线程工厂 // 7. 拒绝策略 pool=new ThreadPoolExecutor(maximumPoolSize-1, maximumPoolSize, 5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(50), Executors.defaultThreadFactory(), new ThreadPoolExecutor.AbortPolicy()); } return pool; } } 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持0133技术站。

以上就是Java如何手动创建线程池的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » Java