当前位置: 首页 > news >正文

用了mip的网站爱客crm登陆

用了mip的网站,爱客crm登陆,广州番禺怎么样,数字营销技术应用网站四种线程池拒绝策略#xff08;handler#xff09; 当线程池的线程数达到最大线程数时#xff0c;需要执行拒绝策略。拒绝策略需要实现 RejectedExecutionHandler 接口#xff0c;并实现 rejectedExecution(Runnable r, ThreadPoolExecutor executor) 方法。不过… 四种线程池拒绝策略handler           当线程池的线程数达到最大线程数时需要执行拒绝策略。拒绝策略需要实现 RejectedExecutionHandler 接口并实现 rejectedExecution(Runnable r, ThreadPoolExecutor executor) 方法。不过 Executors 框架已经为我们实现了 4 种拒绝策略 AbortPolicy默认丢弃任务并抛出 RejectedExecutionException 异常。 CallerRunsPolicy由调用线程处理该任务。 DiscardPolicy丢弃任务但是不抛出异常。可以配合这种模式进行自定义的处理方式。 DiscardOldestPolicy丢弃队列最早的未处理任务然后重新尝试执行任务。 线程池默认的拒绝策略         查看java.util.concurrent.ThreadPoolExecutor类的源码我们可以看到 /*** The default rejected execution handler*/ private static final RejectedExecutionHandler defaultHandler new AbortPolicy(); 线程池的默认拒绝策略为AbortPolicy即丢弃任务并抛出RejectedExecutionException异常。我们可以通过代码来验证这一点现有如下代码 public class ThreadPoolTest {public static void main(String[] args) {BlockingQueueRunnable queue new ArrayBlockingQueue(100);ThreadFactory factory r - new Thread(r, TestThreadPool);ThreadPoolExecutor executor new ThreadPoolExecutor(5, 5,0L, TimeUnit.SECONDS, queue, factory);while (true) {executor.submit(() - {try {System.out.println(queue.size());Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}});}}} 这里是一个默认的线程池没有设置拒绝策略设置了最大线程队列是100。运行代码结果如下 结果是符合预期的这也证明了线程池的默认拒绝策略是ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。 拒绝策略场景分析 1.AbortPolicy         ThreadPoolExecutor.AbortPolicy:丢弃任务并抛出RejectedExecutionException异常。源码解释如下: /*** Creates an {code AbortPolicy}.*/public AbortPolicy() { }/*** Always throws RejectedExecutionException.** param r the runnable task requested to be executed* param e the executor attempting to execute this task* throws RejectedExecutionException always*/ 这是线程池默认的拒绝策略在任务不能再提交的时候抛出异常及时反馈程序运行状态。如果是比较关键的业务推荐使用此拒绝策略这样子在系统不能承载更大的并发量的时候能够及时的通过异常发现。 2.DiscardPolicy         ThreadPoolExecutor.DiscardPolicy丢弃任务但是不抛出异常。如果线程队列已满则后续提交的任务都会被丢弃且是静默丢弃。源码解释如下: /*** Creates a {code DiscardOldestPolicy} for the given executor.*/public DiscardOldestPolicy() { }/*** Obtains and ignores the next task that the executor* would otherwise execute, if one is immediately available,* and then retries execution of task r, unless the executor* is shut down, in which case task r is instead discarded.** param r the runnable task requested to be executed* param e the executor attempting to execute this task*/ 使用此策略可能会使我们无法发现系统的异常状态。建议是一些无关紧要的业务采用此策略。例如某些视频网站统计视频的播放量就是采用的这种拒绝策略。 3.DiscardOldestPolicy         ThreadPoolExecutor.DiscardOldestPolicy丢弃队列最前面的任务然后重新提交被拒绝的任务。源码解释如下: /*** Creates a {code DiscardOldestPolicy} for the given executor.*/public DiscardOldestPolicy() { }/*** Obtains and ignores the next task that the executor* would otherwise execute, if one is immediately available,* and then retries execution of task r, unless the executor* is shut down, in which case task r is instead discarded.** param r the runnable task requested to be executed* param e the executor attempting to execute this task*/ 此拒绝策略是一种喜新厌旧的拒绝策略。是否要采用此种拒绝策略还得根据实际业务是否允许丢弃老任务来认真衡量。 4.CallerRunsPolicy         ThreadPoolExecutor.CallerRunsPolicy由调用线程处理该任务 源码解释如下: /*** Creates a {code CallerRunsPolicy}.*/public CallerRunsPolicy() { }/*** Executes task r in the callers thread, unless the executor* has been shut down, in which case the task is discarded.** param r the runnable task requested to be executed* param e the executor attempting to execute this task*/ 如果任务被拒绝了则由调用线程提交任务的线程直接执行此任务我们可以通过代码来验证这一点 public static void main(String[] args) {BlockingQueueRunnable queue new ArrayBlockingQueue(10);ThreadFactory factory r - new Thread(r, TestThreadPool);ThreadPoolExecutor executor new ThreadPoolExecutor(5, 5,0L, TimeUnit.SECONDS, queue, factory, new ThreadPoolExecutor.CallerRunsPolicy());for (int i 0; i 1000; i) {executor.submit(() - {try {System.out.println(Thread.currentThread().getName() :执行任务);Thread.sleep(1000);} catch (InterruptedException e) {e.printStackTrace();}});} } 把队列最大值改为10打印输出线程的名称。执行结果如下 通过结果可以看到主线程main也执行了任务这正说明了此拒绝策略由调用线程提交任务的线程直接执行被丢弃的任务的。 知识来源 【23版面试突击】你知道线程池有哪几种拒绝策略吗_哔哩哔哩_bilibili 线程池的拒绝策略_线程池拒绝策略_小赵在练琴的博客-CSDN博客
http://www.pierceye.com/news/394260/

相关文章:

  • 东莞营销型网站学动漫设计有前途吗
  • 资讯网站wordpress实例配置
  • 营销网站建设哪里便宜最新房地产新闻
  • 有自己的网站怎么做淘宝客wordpress不自动安装
  • 我自己做网站wcf网站开发
  • 做一个好的网站需要什么店铺设计合同
  • 做网站公司郑州设计师能做网站前端吗
  • 建设工程交易中心网站中国监察报电子版
  • 网站正在建设中 倒计时软文写作范例大全
  • 左中右三栏布局网站建设网站建设微金手指下拉15
  • 做网站公司怎么找数字营销招聘
  • 做网站域名和空间费如何创建一个新网站
  • 前程无忧网广州网站建设类岗位wordpress建站教程视频
  • 徐州建设公司网站最吉祥的公司名字大全
  • wordpress网站前端优化怎么做网站导航地图
  • 成都市武侯区建设局门户网站自助快速建站
  • 专业视频网站开发公司兰州装修公司报价明细表
  • 企业网站管理系统的运维服务建设黑彩网站需要什么
  • 揭阳自助建站大数据就业方向及前景
  • 提供盐城网站开发dreamwearver可以做网站吗
  • 龙岩市建设局网站求大哥给个狼站2022
  • 优化算法 网站让移动网站
  • tomcat 怎么做网站网站免费推广平台
  • 山东定制型网站建设推广上传的网站打不开
  • 定制一个企业网站多少钱东莞网站竞价推广运营
  • o2o的网站有哪些制作简单网页的步骤
  • 东莞网站设计制作教程网站架构的重要性
  • 网站建设 企业观点南阳做网站aokuo
  • 深圳做网站(信科网络)减肥产品网站模板
  • 在线教育网站开发方案wordpress 内存超限