ThreadPoolExecutor 线程池的简单使用

线程池使用
// 初始化一个线程池 
ExecutorService threadPool = new ThreadPoolExecutor(10, 30, 3, TimeUnit.MINUTES, new LinkedBlockingQueue<>(100));

// 使用 
threadPool.submit( () -> { // 执行内容  }

ThreadPoolExecutor 参数

对应参数 (依次)

1、corePoolSize:核心线程数

核心线程会一直存活,即使没有任务需要执行当线程数小于核心线程数时,即使有线程空闲,线程池也会优先创建新线程处理设置allowCoreThreadTimeout=true(默认false)时,核心线程会超时关闭

2、maxPoolSize:最大线程数

当线程数>=corePoolSize,且任务队列已满时。线程池会创建新线程来处理任务
当线程数=maxPoolSize,且任务队列已满时,线程池会拒绝处理任务而抛出异常

3、keepAliveTime:线程空闲时间

当线程数大于核心时,这是多余的空闲线程在终止之前等待新任务的最长时间。

4、unit

时间单位

5、workQueue:在执行任务之前用于保留任务的队列
LinkedBlockingQueue

链表 这个队列接收到任务的时候,如果当前线程数小于核心线程数,则新建线程(核心线程)处理任务;如果当前线程数等于核心线程数,则进入队列等待。超过(最大线程数+队列长度)报错。

new LinkedBlockingQueue<>(20) // 不填参数默认为无限大  Integer.MAX_VALUE
ArrayBlockingQueue

动态数组 有界 takeIndexputIndex 表示存和取的两个数组下标。

这个队列接收到任务的时候,如果当前线程数小于核心线程数,则新建线程(核心线程)处理任务;如果当前线程数等于核心线程数,则进入队列等待。超过(最大线程数+队列长度)报错。

SynchronousQueue 没有容器
/*  
默认为 flase
true ->  new TransferQueue<E>() : 
false -> new TransferStack<E>() 
*/
new SynchronousQueue<>(false)  
DelayQueue

队列内元素必须实现Delayed接口,这就意味着你传进去的任务必须先实现Delayed接口。这个队列接收到任务时,首先先入队,只有达到了指定的延时时间,才会执行任务

CountDownLatch 使用

线程是异步执行的如果需要返回结果,则需要使用 CountDownLatch

// 线程开始执行前 初始计数  
CountDownLatch countDownLatch = new CountDownLatch(20);

// 线程执行中 每执行一次 进行减一 
countDownLatch.countDown();

// 执行后 等待固定时间后返回结果,无论是否执行完成 
boolean await = countDownLatch.await(5, TimeUnit.SECONDS);
if (!await) {
    System.out.println("超时");
}
ThreadPoolExecutor执行顺序

线程池按以下行为执行任务

  1. 当线程数小于核心线程数时,创建线程。
  2. 当线程数大于等于核心线程数,且任务队列未满时,将任务放入任务队列。
  3. 当线程数大于等于核心线程数,且任务队列已满
    • 若线程数小于最大线程数,创建线程
    • 若线程数等于最大线程数,抛出异常,拒绝任务
线程池的状态
  • running状态:当线程池创建后,初始为running 状态
  • shutdown状态:调用shutdown 方法后,处在shutdown 状态,此时不再接受新的任务,等待已有的任务执行完毕
  • stop状态:调用 shutdownnow 方法后,进入 stop 状态,不再接受新的任务,并且会尝试终止正在执行的任务。
  • terminated状态:当处于 shotdown 或 stop 状态,并且所有工作线程已经销毁,任务缓存队列已清空,线程池被设为 terminated 状态。

常用线程池

ScheduledThreadPoolExecutor

延时线程池

ScheduledThreadPoolExecutor scheduled = new ScheduledThreadPoolExecutor(5); 
// 指定核心线程数

scheduled.schedule(() -> {  执行语句; }, 5,  TimeUnit.SECONDS);
//达到给定的延时时间后,执行任务。这里传入的是实现Runnable接口的任务,
//因此通过ScheduledFuture.get()获取结果为null
public ScheduledFuture<?> schedule(Runnable command,
                                       long delay, TimeUnit unit);
//达到给定的延时时间后,执行任务。这里传入的是实现Callable接口的任务,
//因此,返回的是任务的最终计算结果
 public <V> ScheduledFuture<V> schedule(Callable<V> callable,
                                           long delay, TimeUnit unit);

//是以上一个任务开始的时间计时,period时间过去后,a
//检测上一个任务是否执行完毕,如果上一个任务执行完毕,
//则当前任务立即执行,如果上一个任务没有执行完毕,则需要等上一个任务执行完毕后立即执行
public ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
                                                  long initialDelay,
                                                  long period,
                                                  TimeUnit unit);
//当达到延时时间initialDelay后,任务开始执行。上一个任务执行结束后到下一次
//任务执行,中间延时时间间隔为delay。以这种方式,周期性执行任务。
public ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
                                                     long initialDelay,
                                                     long delay,
                                                     TimeUnit unit);
newCachedThreadPool

缓存线程池

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

单线程的线程池

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

定长线程池

public static ExecutorService newFixedThreadPool(int nThreads) {
    return new ThreadPoolExecutor(nThreads, nThreads,
                                  0L, TimeUnit.MILLISECONDS,
                                  new LinkedBlockingQueue<Runnable>());
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ThreadPoolExecutorJava中用于创建和管理线程池的类。它提供了一种方便的方式来执行多个任务,并且可以控制线程的数量和执行方式。 要创建一个ThreadPoolExecutor线程池,可以使用以下步骤: 1. 导入ThreadPoolExecutor类:在代码中导入java.util.concurrent.ThreadPoolExecutor类。 2. 创建ThreadPoolExecutor对象:使用ThreadPoolExecutor的构造函数创建一个线程池对象。构造函数需要传入一些参数,包括核心线程数、最大线程数、线程空闲时间、任务队列等。 3. 提交任务:使用execute()方法或submit()方法向线程池提交任务。execute()方法用于提交不需要返回结果的任务,而submit()方法用于提交需要返回结果的任务。 4. 关闭线程池:在不再需要线程池时,应该调用shutdown()方法来关闭线程池。这会停止接受新任务,并且等待已经提交的任务执行完成。 以下是一个简单的示例代码,演示了如何创建和使用ThreadPoolExecutor线程池: ```java import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; public class ThreadPoolExample { public static void main(String[] args) { // 创建一个线程池,核心线程数为2,最大线程数为5 ExecutorService executor = Executors.newFixedThreadPool(2); // 提交任务到线程池 executor.execute(new Task("Task 1")); executor.execute(new Task("Task 2")); executor.execute(new Task("Task 3")); // 关闭线程池 executor.shutdown(); } static class Task implements Runnable { private String name; public Task(String name) { this.name = name; } @Override public void run() { System.out.println("Executing task: " + name); // 任务逻辑代码 } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值