Thread POOL线程池

什么是线程池?

Thread POOL ,就是将多个线程放入一个池子中,是一种多线程形式,它可以实现线程复用,控制最大的并发数,以及管理线程的整个生命周期。

为什么需要线程池?

频繁的创建和销毁线程非常耗费性能,因为在JAVA中创建一个线程需要调用操作系统的API

Executors 为我们提供了几种创建线程的方式:
newFixedThreadPool
newSingleThreadExecutor:只会创建一个工作线程来处理任务
newCachedThreadPool
newScheduledThreadPool

线程的7个参数

 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:线程工厂
RejectedExecutionHandler :拒绝策略,阻止执行时要使用的处理程序,因为达到了线程边界和队列容量

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CachePool线程池是一种基于缓存机制的线程池,可以提高线程的处理效率和速度。下面是一个自定义的CachePool线程池实现: ```java import java.util.concurrent.*; public class CachePool { private ExecutorService executorService; public CachePool() { int corePoolSize = 0; int maximumPoolSize = Integer.MAX_VALUE; long keepAliveTime = 60L; TimeUnit unit = TimeUnit.SECONDS; BlockingQueue<Runnable> workQueue = new SynchronousQueue<Runnable>(); ThreadFactory threadFactory = Executors.defaultThreadFactory(); RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy(); executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, handler); } public void execute(Runnable task) { executorService.execute(task); } public void shutdown() { executorService.shutdown(); } public boolean isTerminated() { return executorService.isTerminated(); } } ``` 该线程池使用了ThreadPoolExecutor类来实现,其中corePoolSize和maximumPoolSize都设置为最大值Integer.MAX_VALUE,因此线程池中的线程数不会有限制。keepAliveTime设置为60秒,表示线程在空闲60秒后会被回收。workQueue采用了SynchronousQueue,这是一个没有容量限制的阻塞队列,可以让任务立即提交给线程处理,从而避免了任务的排队等待。RejectedExecutionHandler采用了ThreadPoolExecutor.AbortPolicy,表示如果线程池已经达到最大容量,且所有线程都在执行任务,此时再有新任务提交时,会抛出RejectedExecutionException异常,从而避免了线程池过载的情况。execute方法用于提交任务,shutdown方法用于关闭线程池,isTerminated方法用于判断线程池是否已经关闭。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值