zoukankan      html  css  js  c++  java
  • 线程池原理

      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.acc = System.getSecurityManager() == null ?
        null :
        AccessController.getContext();
        this.corePoolSize = corePoolSize;
        this.maximumPoolSize = maximumPoolSize;
        this.workQueue = workQueue;
        this.keepAliveTime = unit.toNanos(keepAliveTime);
        this.threadFactory = threadFactory;
        this.handler = handler;
        }

    corePoolSize 核心线程数;
    maximumPoolSize 最大线程数;
    keepAliveTime 空闲线程数存活时间;
    TimeUnit  时间格式;
    BlockingQueue 工作阻塞队列;
    threadFactory 线程工厂;
    RejectedExecutionHandler 拒绝策略。

    我们知道java中Executors提供了三种创建线程池的方法:

        //默认单个线程
            ExecutorService executorService =Executors.newSingleThreadExecutor();
            //指定线程数
            ExecutorService executorService2 =Executors.newFixedThreadPool(3);
            //带缓存可扩容数最大Integer.MAX_VALUE
            ExecutorService executorService3 =Executors.newCachedThreadPool();

    前两者使用的阻塞队列为LinkedBlockingQueue,链表阻塞队列,虽然是有界队列,

    但是它最大值为Integer.MAX_VALUE,21亿左右,容易产生OOM问题;所以需要

    使用线程池时,需要我们自己手写线程池,并配上对应七大参数;

    其中最大核心数要根据服务环境的cpu数来配置:

    Runtime.getRuntime().availableProcessors();//获取可用cpu核心数

    cpu密集型:core + 1
    io密集型: core / (1 - 阻塞系数)或者core * 2 //阻塞系数(0.8-0.9)

    RejectedExecutionHandler 默认采用

    private static final RejectedExecutionHandler defaultHandler =
            new AbortPolicy();
    

    在超过最大核心数和阻塞队列最大值时会抛出异常,所以不要采用默认拒绝策略。

  • 相关阅读:
    bs4解析错误之 bs4 FeatureNotFound: Couldn't find a tree builder with the features you requested: lxml.
    UI自动化之selenium元素定位不到问题的原因有哪几种?
    selenium定位:出现Message: element not interactable 元素不可交互的问题解决方案
    JS--编码规范
    JS操作数组-2
    JS-数组操作3
    JS操作数组
    用JS解决url地址中参数乱码的问题
    数组去重--ES5和ES6
    选择排序
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/11411332.html
Copyright © 2011-2022 走看看