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.

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/733995.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

C++知识点总结(24):数据结构与栈

数据结构与栈 一、概念1. 数据2. 数据结构3. 数据结构分类 二、栈1. 栈2. 相关概念2.1 入栈2.2 出栈2.3 栈的特点2.4 栈顶2.5 栈底2.6 栈顶元素2.7 栈底元素 三、数组模拟栈1. 初始化空栈2. 入栈3. 出栈4. 获取栈顶元素5. 判断栈是否为空6. 获取栈内元素个数 四、栈的运用1. 括…

[java入门到精通] 11 泛型,数据结构,List,Set

今日目标 泛型使用 数据结构 List Set 1 泛型 1.1 泛型的介绍 泛型是一种类型参数&#xff0c;专门用来保存类型用的 最早接触泛型是在ArrayList&#xff0c;这个E就是所谓的泛型了。使用ArrayList时&#xff0c;只要给E指定某一个类型&#xff0c;里面所有用到泛型的地…

「连载」边缘计算(二十八)03-08:边缘部分源码(源码分析篇)

&#xff08;接上篇&#xff09; eventbus的具体逻辑剖析 从eventbus的启动函数切入分析具体逻辑&#xff0c;具体如下所示。 KubeEdge/edge/pkg/eventbus/event_bus.go func (eb *eventbus) Start(c *context.Context) { // no need to call TopicInit now, we have fixed …

公众号IP白名单已添加服务器IP 122.88... 依然给出 40164 错误

公众号的IP白名单已添加 122.88... 依然给出 40164 错误。 {"errcode":40164,"errmsg":"invalid ip 122.88... ipv6 ::ffff:122.88..., not in whitelist rid: 65e85a07-458dfc0d-16003e03"} 解决方案&#xff1a; 一、检查 AppID 是否正确&…

STM32基本定时功能

1、定时器就是计数器。 2、怎么计数&#xff1f; 3、我们需要有一恒定频率的方波信号&#xff0c;再加上一个寄存器。 4、比如每来一个上升沿信号&#xff0c;寄存器值加1&#xff0c;就可以完成计数。 5、假设方波频率是100Hz&#xff0c;也就是1秒100个脉冲。…

ElasticSearch 模糊查询

前缀搜索 # 前缀搜索 注意&#xff1a; 前缀搜索匹配的是trem,而不是filed&#xff0c;倒排索引的分词 性能比较差&#xff0c;没有缓存 搜索时尽量把前缀词设置长一点 GET /product/_search {"query": {"prefix": {"name": {"value"…

【学习】DLA (Deep Layer Aggregation)

本研究是有由UC Berkeley的Trevor Darrell组发表于2018年CVPR。因为&#xff0c;工作中应用到CenterNet&#xff0c;文章中使用了DLA作为backbone&#xff0c;能够以较高的速度完成推理并维持较高的AP。 DLA文章&#xff1a;论文 DLA 在实际操作中&#xff0c;常常将高级特征…

机器学习-pytorch1(持续更新)

上一节我们学习了机器学习的线性模型和非线性模型的机器学习基础知识&#xff0c;这一节主要将公式变为代码。 代码编写网站&#xff1a;https://colab.research.google.com/drive 学习课程链接&#xff1a;ML 2022 Spring 1、Load Data&#xff08;读取数据&#xff09; 这…

比较字符串

给定程序函数fun的功能是&#xff1a;比较两个字符串&#xff0c;将长的那个字符串的首地址作为函数值返回。 #include <stdio.h> /**********found**********/ char* fun(char *s, char *t) {int s10, t10;char *ss, *tt;sss;ttt; while(*ss){s1;/**********found****…

学习Java的第六天

一、变量 1、变量的定义 在程序执行的过程中变量的值会发生变化&#xff0c;直白来说就是用来存储可变化的数据。从本质上讲&#xff0c;变量其实指的是内存中的一小块存储空间&#xff0c;空间位置是确定的&#xff0c;但是里面放置的值不确定。比如屋子里有多个鞋柜&#x…

启发式算法:禁忌搜索 Tabu Search

文章目录 Tabu searchTabu search算法算例-旅行商TSP问题Tabu search 从一个初始可行解出发,选择一系列的特定搜索方向(移动)作为试探,选择实现让特定的目标函数值变化最多的移动。 为了避免陷入局部最优解,TS搜索中采用了一种灵活的“记忆”技术,对已经进行的优化过程进…

2024年目标检测研究进展

YOLOv9 图片来源网络 YOLO相关的研究&#xff1a;https://blog.csdn.net/yunxinan/article/details/103431338

网络安全: Kali Linux 进行 SSH 渗透与防御

目录 一、实验 1.环境 2.nmap扫描目标主机 3.Kali Linux 进行 SSH 渗透 3.Kali Linux 进行 SSH 防御 二、问题 1.SSH有哪些安全配置 一、实验 1.环境 &#xff08;1&#xff09;主机 表1 主机 系统版本IP备注Kali Linux2022.4 192.168.204.154&#xff08;动态&…

【软考】单元测试

目录 1. 概念2. 测试内容2.1 说明2.2 模块接口2.3 局部数据结构2.4 重要的执行路径 3. 测试过程2.1 说明2.2 单元测试环境图2.3 驱动模块2.4 桩模块 4. 模块接口测试与局部数据结构测试的区别 1. 概念 1.单元测试也称为模块测试&#xff0c;在模块编写完成且无编译错误后就可以…

使用React Context和Hooks在React Native中共享蓝牙数据

使用React Context和Hooks在React Native中共享蓝牙数据 **背景****实现步骤****步骤 1: 创建并导出bleContext****步骤 2: 在App.tsx中使用bleContext.Provider提供数据****步骤 3: 在父组件和子组件中访问共享的数据** **结论** 在开发React Native应用时&#xff0c;跨组件共…

16.Git从入门到进阶

一.Git 初识 1. 概念&#xff1a; 一个免费开源&#xff0c;分布式的代码版本控制系统&#xff0c;帮助开发团队维护代码 2. 作用&#xff1a; 记录代码内容&#xff0c;切换代码版本&#xff0c;多人开发时高效合并代码内容 3. 如何学&#xff1a; 个人本机使用&#xf…

SQL中的不加锁查询 with(nolock)

WITH(NOLOCK) 是一种 SQL Server 中的表提示&#xff08;table hint&#xff09;&#xff0c;可以用来告诉数据库引擎在查询数据时不要加锁&#xff0c;以避免因为锁等待导致查询性能下降。 当多个事务同时访问同一张表时&#xff0c;数据库引擎会对表进行锁定&#xff0c;以确…

数据库中 SQL Hint 是什么?

前言 最近在调研业界其他数据库中 SQL Hint 功能的设计和实现&#xff0c;整体上对 Oracle、Mysql、Postgresql、 Apache Calcite 中的 SQL Hint 的设计和功能都进行了解&#xff0c;这里整理一篇文章来对其进行梳理&#xff0c;一是帮助自己未来回顾&#xff0c;加深自己的思…

Python之Web开发中级教程----搭建Git环境三

Python之Web开发中级教程----搭建Git环境三 多人分布式使用仓库操作实例 场景&#xff1a;开发者A&#xff0c;开发者B在同一个项目协同开发&#xff0c;修改同一个代码文件。开发者A在Win10下&#xff0c;开发者B在Ubuntu下。 1、开发者A修改提交代码 从GitHub: Let’s bu…

44岁「台偶一哥」成现实版「王子变青蛙」,育一子一女成人生赢家

电影《周处除三害》近日热度极高&#xff0c;男主角阮经天被大赞演技出色&#xff0c;最让人意想不到&#xff0c;因为该片在内地票房报捷&#xff0c;很多人走去恭喜另一位台湾男艺人明道&#xff0c;皆因二人出道时外貌神似&#xff0c;至今仍有不少人将两人搞混。 多年过去&…