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();
    

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

  • 相关阅读:
    Python多线程爬虫爬取电影天堂资源
    BGP协议初探
    dubbo序列化hibernate.LazyInitializationException could not initialize proxy
    Spring Boot打包war jar 部署tomcat
    HSSFClientAnchor 参数说明
    attempt to create delete event with null entity
    解决BUG:CS1617: 选项“6”对 /langversion 无效;必须是 ISO-1、ISO-2、3、4、5 或 Default
    Android Studio :enable vt-x in your bios security,已经打开还是报错的解决方法
    visual studio 2015 开发android
    ASP.NET用QQ,网易发送邮件以及添加附件
  • 原文地址:https://www.cnblogs.com/SimonHu1993/p/11411332.html
Copyright © 2011-2022 走看看