ExecutorService

2024-03-09 13:52
文章标签 executorservice

本文主要是介绍ExecutorService,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

常用ExecutorServicecurrentThreadSize < coreThreadSize --> create a new thread

currentThreadSize = coreThreadSize --> queue submitted task

queue submitted task is full & currentThreadSize <maxPoolSize --> create a new thread

queue is full & currentThreadSize = maxPoolSize --> reject by policy.

​​​​​​​int parallelism = Runtime.getRuntime().availableProcessors();
RejectedExecutionHandler rejectHandler = new ThreadPoolExecutor.CallerRunsPolicy();
this.executorService = new ThreadPoolExecutor(parallelism, parallelism + 2,60_000L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(500), rejectHandler);

Core and maximum pool sizes
A ThreadPoolExecutor will automatically adjust the pool size (see getPoolSize) according to the bounds set by corePoolSize (see getCorePoolSize) and maximumPoolSize (see getMaximumPoolSize). When a new task is submitted in method execute(Runnable), and fewer than corePoolSize threads are running, a new thread is created to handle the request, even if other worker threads are idle. If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full. By setting corePoolSize and maximumPoolSize the same, you create a fixed-size thread pool. By setting maximumPoolSize to an essentially unbounded value such as Integer.MAX_VALUE, you allow the pool to accommodate an arbitrary number of concurrent tasks. Most typically, core and maximum pool sizes are set only upon construction, but they may also be changed dynamically using setCorePoolSize and setMaximumPoolSize.
On-demand construction
By default, even core threads are initially created and started only when new tasks arrive, but this can be overridden dynamically using method prestartCoreThread or prestartAllCoreThreads. You probably want to prestart threads if you construct the pool with a non-empty queue.
Creating new threads
New threads are created using a ThreadFactory. If not otherwise specified, a Executors.defaultThreadFactory is used, that creates threads to all be in the same ThreadGroup and with the same NORM_PRIORITY priority and non-daemon status. By supplying a different ThreadFactory, you can alter the thread's name, thread group, priority, daemon status, etc. If a ThreadFactory fails to create a thread when asked by returning null from newThread, the executor will continue, but might not be able to execute any tasks. Threads should possess the "modifyThread" RuntimePermission. If worker threads or other threads using the pool do not possess this permission, service may be degraded: configuration changes may not take effect in a timely manner, and a shutdown pool may remain in a state in which termination is possible but not completed.
Keep-alive times
If the pool currently has more than corePoolSize threads, excess threads will be terminated if they have been idle for more than the keepAliveTime (see getKeepAliveTime(TimeUnit)). This provides a means of reducing resource consumption when the pool is not being actively used. If the pool becomes more active later, new threads will be constructed. This parameter can also be changed dynamically using method setKeepAliveTime(long, TimeUnit). Using a value of Long.MAX_VALUE TimeUnit.NANOSECONDS effectively disables idle threads from ever terminating prior to shut down. By default, the keep-alive policy applies only when there are more than corePoolSize threads. But method allowCoreThreadTimeOut(boolean) can be used to apply this time-out policy to core threads as well, so long as the keepAliveTime value is non-zero.

Queuing
Any BlockingQueue may be used to transfer and hold submitted tasks. The use of this queue interacts with pool sizing:
If fewer than corePoolSize threads are running, the Executor always prefers adding a new thread rather than queuing.
If corePoolSize or more threads are running, the Executor always prefers queuing a request rather than adding a new thread.
If a request cannot be queued, a new thread is created unless this would exceed maximumPoolSize, in which case, the task will be rejected.
There are three general strategies for queuing:
Direct handoffs. A good default choice for a work queue is a SynchronousQueue that hands off tasks to threads without otherwise holding them. Here, an attempt to queue a task will fail if no threads are immediately available to run it, so a new thread will be constructed. This policy avoids lockups when handling sets of requests that might have internal dependencies. Direct handoffs generally require unbounded maximumPoolSizes to avoid rejection of new submitted tasks. This in turn admits the possibility of unbounded thread growth when commands continue to arrive on average faster than they can be processed.
Unbounded queues. Using an unbounded queue (for example a LinkedBlockingQueue without a predefined capacity) will cause new tasks to wait in the queue when all corePoolSize threads are busy. Thus, no more than corePoolSize threads will ever be created. (And the value of the maximumPoolSize therefore doesn't have any effect.) This may be appropriate when each task is completely independent of others, so tasks cannot affect each others execution; for example, in a web page server. While this style of queuing can be useful in smoothing out transient bursts of requests, it admits the possibility of unbounded work queue growth when commands continue to arrive on average faster than they can be processed.
Bounded queues. A bounded queue (for example, an ArrayBlockingQueue) helps prevent resource exhaustion when used with finite maximumPoolSizes, but can be more difficult to tune and control. Queue sizes and maximum pool sizes may be traded off for each other: Using large queues and small pools minimizes CPU usage, OS resources, and context-switching overhead, but can lead to artificially low throughput. If tasks frequently block (for example if they are I/O bound), a system may be able to schedule time for more threads than you otherwise allow. Use of small queues generally requires larger pool sizes, which keeps CPUs busier but may encounter unacceptable scheduling overhead, which also decreases throughput.

Rejected tasks
New tasks submitted in method execute(Runnable) will be rejected when the Executor has been shut down, and also when the Executor uses finite bounds for both maximum threads and work queue capacity, and is saturated. In either case, the execute method invokes the RejectedExecutionHandler.rejectedExecution(Runnable, ThreadPoolExecutor) method of its RejectedExecutionHandler. Four predefined handler policies are provided:
In the default ThreadPoolExecutor.AbortPolicy, the handler throws a runtime RejectedExecutionException upon rejection.
In ThreadPoolExecutor.CallerRunsPolicy, the thread that invokes execute itself runs the task. This provides a simple feedback control mechanism that will slow down the rate that new tasks are submitted.
In ThreadPoolExecutor.DiscardPolicy, a task that cannot be executed is simply dropped.
In ThreadPoolExecutor.DiscardOldestPolicy, if the executor is not shut down, the task at the head of the work queue is dropped, and then execution is retried (which can fail again, causing this to be repeated.)
It is possible to define and use other kinds of RejectedExecutionHandler classes. Doing so requires some care especially when policies are designed to work only under particular capacity or queuing policies.

这篇关于ExecutorService的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/790870

相关文章

ExecutorService.invokeAny()和ExecutorService.invokeAll()的使用剖析

ExecutorService.invokeAny()和ExecutorService.invokeAll()的使用剖析 时间 2014-07-27 16:15:07   CSDN博客 原文   http://blog.csdn.net/aitangyong/article/details/38172189 主题  Java ExecutorService是JDK并发工

Java零基础-线程池(`ExecutorService`)的使用

哈喽,各位小伙伴们,你们好呀,我是喵手。运营社区:C站/掘金/腾讯云/阿里云/华为云/51CTO;欢迎大家常来逛逛。   今天我要给大家分享一些自己日常学习到的一些知识点,并以文字的形式跟大家一起交流,互相学习,一个人虽可以走的更快,但一群人可以走的更远。   我是一名后端开发爱好者,工作日常接触到最多的就是Java语言啦,所以我都尽量抽业余时间把自己所学到所会的,通过文章的形式进行输出

线程池原理–执行器ExecutorService

文章目录 线程池原理–执行器ExecutorService相关方法shutdownsubmit批量提交任务 线程池原理–总索引 线程池原理–执行器ExecutorService ExecutorService 也是一个接口,继承Executor接口。 java interface ExecutorService extends Executor 相关方法 shu

Java 并发学习之ExecutorService

在Java5之后,并发线程这块发生了根本的变化,最重要的莫过于新的启动、调度、管理线程的一大堆API了。在Java5以后,通过Executor来启动线程比用Thread的start()更好。在新特征中,可以很容易控制线程的启动、执行和关闭过程,还可以很容易使用线程池的特性 一、创建任务 任务就是一个实现了Runnable接口的类。 创建的时候实run方法即可。 二、执行任务 通过java.util

[Java] CountDownLatch和ExecutorService的简单使用

文章目录 [Java] CountDownLatch和ExecutorService的简单使用一、前言二、记录 [Java] CountDownLatch和ExecutorService的简单使用 一、前言 环境说明: JDK:1.8 官方API文档:https://docs.oracle.com/javase/8/docs/api/index.html 参考:

ExecutorService引发的血案(三)ThreadPoolExecutor

前面我们提到了ExecutorService结构中的一个工厂类,Executors。这个类提供了一系列构造ExecutorService实例的方法。 这些方法的核心就是两个类,分别是 ThreadPoolExecutor 和 ScheduledThreadPoolExecutor 类。(当然还有别的类,比较常用的就是这两个) 今天介绍的就是 ThreadPoolExecutor。 简介 这

ExecutorService引发的血案(二)ExecutorService使用

上一节中讲到了ExecutorService中有一些管理Thread的方法 execute(Runnable)submit(Runnable)submit(Callable)invokeAny(...)invokeAll(...) execute(Runnable) 这个方法使用的参数是 java.lang.Runnable 包中的对象,调用这个方法之后将会异步执行runnable。

ExecutorService引发的血案(一)结构

最近使用了github上面的一个多线程下载的库 MultiThreadDownloader,发现挺好用。于是打开看了一下源码,发现了ExecutorService这个东西。之前多多少少接触到了这个东西,知道是java并发编程里面使用的,但是一直不是很了解所以花点时间看了一下。 下面这张图,就是ExecutorService的结构图 (ScheduledThreadPoolExecutor稍

Runnable,Callable,Future,RunnableFuture,FutureTask,ExecutorService的关系

关系 Executor就是Runnable和Callable的调度容器,Future就是对于具体的调度任务的执行结果进行查看,最为关键的是Future可以检查对应的任务是否已经完成,也可以阻塞在get方法上一直等待任务返回结果。Runnable和Callable的差别就是Runnable是没有结果可以返回的,就算是通过Future也看不到任务调度的结果的。  FutureTask则是一个R

多线程--ExecutorService

相比ExecutorService,CompletionService可以更精确和简便地完成异步任务的执行 CompletionService的一个实现是ExecutorCompletionService,它是Executor和BlockingQueue功能的融合体,Executor完成计算任务,BlockingQueue负责保存异步任务的执行结果。 ExecutorCompletionSer