1.newFixedThreadPool
newFixedThreadPool
是 Executors
工具类提供的一个静态方法,用于创建一个固定大小的线程池。该线程池具有固定数量的核心线程,且没有最大线程数限制。如果所有的核心线程都在执行任务,新任务会被放入队列中等待执行。这种线程池适用于需要控制并发线程数量的场景,而且相比于动态调整线程数量的线程池,更容易掌控。
public static ExecutorService newFixedThreadPool(int nThreads) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>());
}public static ExecutorService newFixedThreadPool(int nThreads, ThreadFactory threadFactory) {return new ThreadPoolExecutor(nThreads, nThreads,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>(),threadFactory);
}
corePoolSize(核心线程数) | nThreads(自定义) |
maximumPoolSize(最大线程数) | 0 |
keepAliveTime(线程空闲时间) | 0 |
unit(时间单位) | 忽略 |
workQueue(任务队列) | 无界队列(LinkedBlockingQueue) |
threadFactory(线程工厂) | threadFactory或defaultThreadFactory |
handler(拒绝策略) | AbortPolicy 是默认的拒绝策略,它会抛出 RejectedExecutionException 异常,表示拒绝接受新任务。 |
2.newSingleThreadExecutor
newSingleThreadExecutor
是 Executors
工具类提供的一个静态方法,用于创建一个只有一个工作线程的线程池。该线程池的核心线程数和最大线程数都为1,因此它只能顺序执行任务,不会并发执行。这种线程池适用于需要顺序执行任务的场景,确保任务按照提交的顺序被执行。
public static ExecutorService newSingleThreadExecutor() {return new FinalizableDelegatedExecutorService(new ThreadPoolExecutor(1, 1,0L, TimeUnit.MILLISECONDS,new LinkedBlockingQueue<Runnable>()));
}
corePoolSize(核心线程数) | 1 |
maximumPoolSize(最大线程数) | 1 |
keepAliveTime(线程空闲时间) | 0 |
unit(时间单位) | 忽略 |
workQueue(任务队列) | 无界队列(LinkedBlockingQueue) |
threadFactory(线程工厂) | threadFactory或defaultThreadFactory |
handler(拒绝策略) | AbortPolicy 是默认的拒绝策略,它会抛出 RejectedExecutionException 异常,表示拒绝接受新任务。 |
3.newCachedThreadPool
newCachedThreadPool
是 Executors
工具类提供的一个静态方法,用于创建一个具有自动调整大小的线程池。该线程池的核心线程数为0,最大线程数为 Integer.MAX_VALUE
,线程空闲时间为60秒。这种线程池适用于执行很多短期异步任务的场景,其中线程池的大小需要根据当前任务的数量进行动态调整。
public static ExecutorService newCachedThreadPool() {return new ThreadPoolExecutor(0, Integer.MAX_VALUE,60L, TimeUnit.SECONDS,new SynchronousQueue<Runnable>());
}
corePoolSize(核心线程数) | 0 |
maximumPoolSize(最大线程数) | Integer.MAX_VALUE |
keepAliveTime(线程空闲时间) | 60 |
unit(时间单位) | s |
workQueue(任务队列) | 直接提交队列(SynchronousQueue) |
threadFactory(线程工厂) | threadFactory或defaultThreadFactory |
handler(拒绝策略) | AbortPolicy 是默认的拒绝策略,它会抛出 RejectedExecutionException 异常,表示拒绝接受新任务。 |
4.newScheduledThreadPool
newScheduledThreadPool
是 Executors
工具类提供的一个静态方法,用于创建一个具有固定大小的、支持定时及周期性任务执行的线程池。该线程池的核心线程数是固定的,而且线程池支持定时任务(ScheduledTask
)的执行,例如定时执行任务或以固定的周期执行任务。
public ScheduledThreadPoolExecutor(int corePoolSize) {super(corePoolSize, Integer.MAX_VALUE,DEFAULT_KEEPALIVE_MILLIS, MILLISECONDS,new DelayedWorkQueue());
}
corePoolSize(核心线程数) | corePoolSize |
maximumPoolSize(最大线程数) | Integer.MAX_VALUE |
keepAliveTime(线程空闲时间) | 10 |
unit(时间单位) | MILLISECONDS |
workQueue(任务队列) | DelayedWorkQueue |
threadFactory(线程工厂) | threadFactory或defaultThreadFactory |
handler(拒绝策略) | AbortPolicy 是默认的拒绝策略,它会抛出 RejectedExecutionException 异常,表示拒绝接受新任务。 |
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;public class ScheduledThreadPoolExample {public static void main(String[] args) {// 创建一个具有固定大小的、支持定时及周期性任务执行的线程池,大小为3ScheduledExecutorService executor = Executors.newScheduledThreadPool(3);// 定时执行任务executor.schedule(() -> {System.out.println("Task 1 is running on thread " + Thread.currentThread().getName());}, 2, TimeUnit.SECONDS);// 周期性执行任务,每隔3秒执行一次executor.scheduleAtFixedRate(() -> {System.out.println("Task 2 is running on thread " + Thread.currentThread().getName());}, 0, 3, TimeUnit.SECONDS);// 关闭线程池executor.shutdown();}
}
DelayedWorkQueue
DelayedWorkQueue
并不是一个具体的线程池工作队列的类,而是一个接口,属于 Java 中的 java.util.concurrent
包。该接口继承自 BlockingQueue
接口,用于表示支持延迟元素的工作队列。
在 Java 中,延迟队列(DelayedQueue
)是一种特殊的队列,其中的元素只有在其指定的延迟时间过去后才能够被消费。DelayedWorkQueue
接口定义了对延迟元素进行操作的方法,常见的实现类是 DelayQueue
。
以下是 DelayedWorkQueue
接口的主要方法:
put(E e)
:
- 将指定的元素插入到队列中,如果需要的话将阻塞等待直到元素可用或者队列被关闭。
poll()
:- 获取并移除队列中的头元素,如果队列为空,则返回
null
。 poll(long timeout, TimeUnit unit)
:- 获取并移除队列中的头元素,如果队列为空,则等待指定的时间,如果超时还未有可用元素,则返回
null
。 peek()
:- 获取但不移除队列的头元素,如果队列为空,则返回
null
。 size()
:- 返回队列中的元素数量。
DelayedWorkQueue
主要用于在具有时间限制的任务调度场景中,例如实现定时任务的执行。实现了 Delayed
接口的元素必须提供一个 getDelay
方法,该方法返回该元素还需要延迟的时间。