线程池

网友投稿 837 2022-11-05

线程池

线程池

线程池简单使用

先看一段代码

package ThreadTest;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;public class PoolDemo { public static void main(String[] args) { //单一线程的线程池 //ExecutorService executorService = Executors.newSingleThreadExecutor(); //固定个数线程的线程池 //ExecutorService executorService = Executors.newFixedThreadPool(5); //可变长度的线程池 ExecutorService executorService = Executors.newCachedThreadPool(); try { for (int i = 1; i <= 100; i++) { int finalI = i; executorService.execute(()->{ System.out.println(Thread.currentThread().getName() + " " + finalI); }); } } catch (Exception e) { e.printStackTrace(); } finally { executorService.shutdown(); } }}

线程池的七个参数

将上面线程池的源码找出来:

newSIngleThreadExecutor

newFixedThreadPool

newCachedThreadPool

public ThreadPoolExecutor(int corePoolSize,//核心线程池大小 int maximumPoolSize,//最大线程池大小 long keepAliveTime,//超时了没有人调用就会释放(非核心区) TimeUnit unit,//超时时间单位 BlockingQueue 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.corePoolSize = corePoolSize; this.maximumPoolSize = maximumPoolSize; this.workQueue = workQueue; this.keepAliveTime = unit.toNanos(keepAliveTime); this.threadFactory = threadFactory; this.handler = handler;}

主要是给自己看的,所以肯定会出现很多错误哈哈哈哈哈

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:VMware NSX原理与实践----NSX Edge实现的其他功能
下一篇:Mitt: 微小的函数事件emitter / pubsub
相关文章

 发表评论

暂时没有评论,来抢沙发吧~