Spring Boot中使用Java线程池ExecutorService

1. 认识java线程池

1.1 在什么情况下使用线程池?

  • 1.单个任务处理的时间比较短
  • 2.需处理的任务的数量大

1.2 使用线程池的好处:

  • 1.减少在创建和销毁线程上所花的时间以及系统资源的开销
  • 2.如不使用线程池,有可能造成系统创建大量线程而导致消耗完系统内存

1.3 线程池包括以下四个基本组成部分:

  • 1、线程池管理器(ThreadPool):用于创建并管理线程池,包括 创建线程池,销毁线程池,添加新任务;
  • 2、工作线程(PoolWorker):线程池中线程,在没有任务时处于等待状态,可以循环的执行任务;
  • 3、任务接口(Task):每个任务必须实现的接口,以供工作线程调度任务的执行,
  • 它主要规定了任务的入口,任务执行完后的收尾工作,任务的执行状态等;
  • 4、任务队列(taskQueue):用于存放没有处理的任务。提供一种缓冲机制。

1.4 线程池的核心参数

ThreadPoolExecutor 有四个构造方法,前三个都是调用最后一个(最后一个参数最全)

public ThreadPoolExecutor(int corePoolSize,
              int maximumPoolSize,
              long keepAliveTime,
              TimeUnit unit,
              BlockingQueue<Runnable> workQueue) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
      Executors.defaultThreadFactory(), defaultHandler);
 }
 public ThreadPoolExecutor(int corePoolSize,
              int maximumPoolSize,
              long keepAliveTime,
              TimeUnit unit,
              BlockingQueue<Runnable> workQueue,
              ThreadFactory threadFactory) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
      threadFactory, defaultHandler);
 }
 public ThreadPoolExecutor(int corePoolSize,
              int maximumPoolSize,
              long keepAliveTime,
              TimeUnit unit,
              BlockingQueue<Runnable> workQueue,
              RejectedExecutionHandler handler) {
   this(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
      Executors.defaultThreadFactory(), handler);
 }
 // 都调用它
 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.corePoolSize = corePoolSize;
   this.maximumPoolSize = maximumPoolSize;
   this.workQueue = workQueue;
   this.keepAliveTime = unit.toNanos(keepAliveTime);
   this.threadFactory = threadFactory;
   this.handler = handler;
 }

主要参数

corePoolSize:核心线程数

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

maxPoolSize:最大线程数

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

keepAliveTime:线程空闲时间

  • 当线程空闲时间达到keepAliveTime时,线程会退出,直到线程数量=corePoolSize
  • 如果allowCoreThreadTimeout=true,则会直到线程数量=0

workQueue:一个阻塞队列,用来存储等待执行的任务,这个参数的选择也很重要,

会对线程池的运行过程产生重大影响,一般来说,这里的阻塞队列有以下几种选择:

  • ArrayBlockingQueue;
  • LinkedBlockingQueue;
  • SynchronousQueue;

关于阻塞队列可以看这篇:java 阻塞队列

threadFactory:线程工厂,主要用来创建线程;

rejectedExecutionHandler:任务拒绝处理器,两种情况会拒绝处理任务:

  • 当线程数已经达到maxPoolSize,切队列已满,会拒绝新任务
  • 当线程池被调用shutdown()后,会等待线程池里的任务执行完毕,再shutdown。
  • 如果在调用shutdown()和线程池真正shutdown之间提交任务,会拒绝新任务

当拒绝处理任务时线程池会调用rejectedExecutionHandler来处理这个任务。如果没有设置默认是AbortPolicy,

会抛出异常。ThreadPoolExecutor类有几个内部实现类来处理这类情况:

  • AbortPolicy 丢弃任务,抛运行时异常
  • CallerRunsPolicy 执行任务
  • DiscardPolicy 忽视,什么都不会发生
  • DiscardOldestPolicy 从队列中踢出最先进入队列(最后一个执行)的任务
  • 实现RejectedExecutionHandler接口,可自定义处理器

1.5 Java线程池 ExecutorService

  • Executors.newCachedThreadPool 创建一个可缓存线程池,如果线程池长度超过处理需要,
  • 可灵活回收空闲线程,若无可回收,则新建线程。
  • Executors.newFixedThreadPool 创建一个定长线程池,可控制线程最大并发数,
  • 超出的线程会在队列中等待。
  • Executors.newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行。
  • Executors.newSingleThreadExecutor 创建一个单线程化的线程池,
  • 它只会用唯一的工作线程来执行任务,保证所有任务按照指定顺序(FIFO, LIFO, 优先级)执行。

备注:Executors只是一个工厂类,它所有的方法返回的都是ThreadPoolExecutor、

ScheduledThreadPoolExecutor这两个类的实例。

1.6 ExecutorService有如下几个执行方法

  • executorService.execute(Runnable);这个方法接收一个Runnable实例,并且异步的执行
  • executorService.submit(Runnable)
  • executorService.submit(Callable)
  • executorService.invokeAny(…)
  • executorService.invokeAll(…)

execute(Runnable)

这个方法接收一个Runnable实例,并且异步的执行

executorService.execute(new Runnable() {
public void run() {
  System.out.println("Asynchronous task");
}
});
executorService.shutdown();

submit(Runnable)

submit(Runnable)和execute(Runnable)区别是前者可以返回一个Future对象,

通过返回的Future对象,我们可以检查提交的任务是否执行完毕,请看下面执行的例子:

Future future = executorService.submit(new Runnable() {
public void run() {
  System.out.println("Asynchronous task");
}
});
future.get(); //returns null if the task has finished correctly.

submit(Callable)

submit(Callable)和submit(Runnable)类似,也会返回一个Future对象,但是除此之外,

submit(Callable)接收的是一个Callable的实现,Callable接口中的call()方法有一个返回值,

可以返回任务的执行结果,而Runnable接口中的run()方法是void的,没有返回值。请看下面实例:

Future future = executorService.submit(new Callable(){
public Object call() throws Exception {
  System.out.println("Asynchronous Callable");
  return "Callable Result";
}
});
System.out.println("future.get() = " + future.get());

如果任务执行完成,future.get()方法会返回Callable任务的执行结果。注意,future.get()方法会产生阻塞。

invokeAny(…)

invokeAny(…)方法接收的是一个Callable的集合,执行这个方法不会返回Future,

但是会返回所有Callable任务中其中一个任务的执行结果。这个方法也无法保证返回的是哪个任务的执行结果,

反正是其中的某一个。

 
ExecutorService executorService = Executors.newSingleThreadExecutor();
Set<Callable<String>> callables = new HashSet<Callable<String>>();
callables.add(new Callable<String>() {
public String call() throws Exception {
  return "Task 1";
}
});
callables.add(new Callable<String>() {
public String call() throws Exception {
  return "Task 2";
}
});
callables.add(new Callable<String>() {
  public String call() throws Exception {
  return "Task 3";
}
});
String result = executorService.invokeAny(callables);
System.out.println("result = " + result);
executorService.shutdown();

invokeAll(…)

invokeAll(…)与 invokeAny(…)类似也是接收一个Callable集合,

但是前者执行之后会返回一个Future的List,其中对应着每个Callable任务执行后的Future对象。

List<Future<String>> futures = executorService.invokeAll(callables);
for(Future<String> future : futures){
System.out.println("future.get = " + future.get());
}
executorService.shutdown();

callable有返回值:

import java.util.*;
import java.util.concurrent.*;
import static java.util.Arrays.asList;

public class Sums {
    // Callable,有返回值
    static class Sum implements Callable<Long> {
        // 传入参数: from 和 to
        private final long from;
        private final long to;
        // 从带参的构造器这里获得传入的参数:from 和 to
        Sum(long from, long to) {
            this.from = from;
            this.to = to;
        }
        // 返回Long类型的自加结果
        @Override
        public Long call() {
            long acc = 0;
            // 从0开始的自加循环,从from计数到to
            for (long i = from; i <= to; i++) {
                acc = acc + i;
            }
            return acc;
        }      
    }
    
    public static void main(String[] args) throws Exception {
        
        ExecutorService executor = Executors.newFixedThreadPool(2);
        List <Future<Long>> results = executor.invokeAll(asList(
            new Sum(0, 10), new Sum(100, 1_000), new Sum(10_000, 1_000_000)
        ));
        executor.shutdown();
        
        for (Future<Long> result : results) {
            System.out.println(result.get());
        }      
    }  
}

2.在springBoot中使用java线程池ExecutorService

2.1 springBoot 的使用配置

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
 * 数据收集配置,主要作用在于Spring启动时自动加载一个ExecutorService对象.
 * @author Bruce
 * @date 2017/2/22
 * update by Cliff at 2027/11/03
 */
@Configuration
public class ThreadPoolConfig {
  @Bean
  public ExecutorService getThreadPool(){
    return Executors.newFixedThreadPool();
  }
}

2.2 使用

在@service 中注入 ExecutorService 然后就可以直接用了。
  @Autowired
  private ExecutorService executorService;
public void test(){
    executorService.execute(new Runnable() {
      public void run() {
        System.out.println("Asynchronous task");
      }
    });
  }

资料来自:https://www.cnblogs.com/lanpo/articles/12391274.html, https://www.manongdao.com/article-2415130.html

原文地址:https://www.cnblogs.com/hanzeng1993/p/15693336.html