热门标签 | HotTags
当前位置:  开发笔记 > 运维 > 正文

java线程池keepAliveTime的含义说明

这篇文章主要介绍了java线程池keepAliveTime的含义说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

之前对线程池中属性:keepAliveTime比较模糊,而且看过之后过一段时间就会忘掉,于是就在此记录一下。

keepAliveTime的jdk中的解释为:

当线程数大于核心时,此为终止前多余的空闲线程等待新任务的最长时间。

说的让人感觉比较模糊,总结一下大概意思为:比如说线程池中最大的线程数为50,而其中只有40个线程任务在跑,相当于有10个空闲线程,这10个空闲线程不能让他一直在开着,因为线程的存在也会特别好资源的,所有就需要设置一个这个空闲线程的存活时间,这么解释应该就很清楚了。

这样以后忘记了就过来看看就OK了。

补充:线程池的状态及KeepAliveTime参数

五个状态

 // runState is stored in the high-order bits
 private static final int RUNNING = -1 <

循环getTask方法

/**
  * Performs blocking or timed wait for a task, depending on
  * current configuration settings, or returns null if this worker
  * must exit because of any of:
  * 1. There are more than maximumPoolSize workers (due to
  * a call to setMaximumPoolSize).
  * 2. The pool is stopped.
  * 3. The pool is shutdown and the queue is empty.
  * 4. This worker timed out waiting for a task, and timed-out
  * workers are subject to termination (that is,
  * {@code allowCoreThreadTimeOut || workerCount > corePoolSize})
  * both before and after the timed wait.
  *
  * @return task, or null if the worker must exit, in which case
  *   workerCount is decremented
  */
 private Runnable getTask() {
  boolean timedOut = false; // Did the last poll() time out&#63;
  retry:
  for (;;) {
   int c = ctl.get();
   int rs = runStateOf(c);
   // Check if queue empty only if necessary.
   if (rs >= SHUTDOWN && (rs >= STOP || workQueue.isEmpty())) {
    decrementWorkerCount();
    return null;
   }
   boolean timed;  // Are workers subject to culling&#63;
   for (;;) {
    int wc = workerCountOf(c);
    timed = allowCoreThreadTimeOut || wc > corePoolSize;
    //默认allowCoreThreadTimeOut为false,除非程序指定
    //(1)当没有超过核心线程时,默认allowCoreThreadTimeOut为false时
    //timed值为false,始终break掉,不会销毁线程
    //(2)当超过核心线程数,默认allowCoreThreadTimeOut为false时
    //timed值为true,如果超过最大值,则销毁;如果timeout过,则销毁
    // 如果allowCoreThreadTimeOut为true,则timed始终为true
    if (wc <= maximumPoolSize && ! (timedOut && timed))
     break;
    if (compareAndDecrementWorkerCount(c))
     return null;
    c = ctl.get(); // Re-read ctl
    if (runStateOf(c) != rs)
     continue retry;
    // else CAS failed due to workerCount change; retry inner loop
   }
   try {
    Runnable r = timed &#63;
     workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS) :
     workQueue.take();
    if (r != null)
     return r;
    timedOut = true;
   } catch (InterruptedException retry) {
    timedOut = false;
   }
  }
 }

线程池状态大于SHUTDOWN值的两种情况

1、调用shutdown方法

当线程池调用了shutdown方法,线程池的状态会首先被设置为SHUTDOWN,然后遍历线程池中所有线程,调用一次interrupt方法,如果在休眠中的线程将会激活,激活后的线程以及调用shutdown方法本身的线程都会尝试去调用tryTerminate方法,该方法将判定如果线程池中所有记录的线程数为0,则将线程状态改为TERMINATED,这个值为3,将大于SHUTDOWN状态值。

2、调用shutdownNow方法

当线程调用了shutdownNow方法后,首先将线程的状态修改为STOP,这个状态是大于SHUTDOWN值的,接下来它也会通过中断激活线程,只是它来的更暴力一些,连加锁和一些基本判断都没有,直接中断;在调用tryTerminate之前会先清空阻塞队列中所有的元素,这些元素被组装为一个List列表作为shutdownNow方法的返回值。换句话说,没有执行的任务在shutdownNow执行后的返回值中可以得到。在程序某些必要的情况下,可以通过线程池的isTerminating,isTerminated,isStopped,isShutdown来对线程做一些状态判定。

KeepAliveTime参数

workQueue.poll(keepAliveTime, TimeUnit.NANOSECONDS)

当阻塞队列中没有任务时,等待时间达到keepAliveTime毫秒值时就会被自动唤醒,而不会永远地沉睡下去。

keepAliveTime,如果是通过newCachedThreadPool的话,默认是1分钟超时,如果遇到前面所提到的瞬间冲击,那么线程池数量将瞬间快速膨胀,而且这些瞬间膨胀的线程的生命周期最少在1分钟以上。

如果设置了该参数,那么当timeout的时候,就return null,就会跳出循环,回收线程。

if (wc <= maximumPoolSize && ! (timedOut && timed))
     break;
    if (compareAndDecrementWorkerCount(c))
     return null;

allowCoreThreadTimeout : 默认情况下核心线程不会退出,可通过将该参数设置为true,让核心线程也退出。

默认的Executors工厂,只有newCachedThreadPool,timeout为60秒,出现timeout情况下,而且线程数超过了核心线程数,会销毁销毁线程。保持在corePoolSize数(如果是cached的,corePoolSize为0)。

 /**
  * Timeout in nanoseconds for idle threads waiting for work.
  * Threads use this timeout when there are more than corePoolSize
  * present or if allowCoreThreadTimeOut. Otherwise they wait
  * forever for new work.
  */
 private volatile long keepAliveTime;
 /**
  * If false (default), core threads stay alive even when idle.
  * If true, core threads use keepAliveTime to time out waiting
  * for work.
  */
 private volatile boolean allowCoreThreadTimeOut;

线程池最小是corePoolSize,最大是maximumPoolSize,除非设置了allowCoreThreadTimeOut和超时时间,这种情况线程数可能减少到0,最大可能是Integer.MAX_VALUE。

Core pool size is the minimum number of workers to keep alive(and not allow to time out etc) unless allowCoreThreadTimeOut is set, in which case the minimum is zero.

/**
  * Creates a thread pool that creates new threads as needed, but
  * will reuse previously constructed threads when they are
  * available. These pools will typically improve the performance
  * of programs that execute many short-lived asynchronous tasks.
  * Calls to execute will reuse previously constructed
  * threads if available. If no existing thread is available, a new
  * thread will be created and added to the pool. Threads that have
  * not been used for sixty seconds are terminated and removed from
  * the cache. Thus, a pool that remains idle for long enough will
  * not consume any resources. Note that pools with similar
  * properties but different details (for example, timeout parameters)
  * may be created using {@link ThreadPoolExecutor} constructors.
  *
  * @return the newly created thread pool
  */
 public static ExecutorService newCachedThreadPool() {
  return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
          60L, TimeUnit.SECONDS,
          new SynchronousQueue());
 }
 /**
  * Creates a thread pool that creates new threads as needed, but
  * will reuse previously constructed threads when they are
  * available, and uses the provided
  * ThreadFactory to create new threads when needed.
  * @param threadFactory the factory to use when creating new threads
  * @return the newly created thread pool
  * @throws NullPointerException if threadFactory is null
  */
 public static ExecutorService newCachedThreadPool(ThreadFactory threadFactory) {
  return new ThreadPoolExecutor(0, Integer.MAX_VALUE,
          60L, TimeUnit.SECONDS,
          new SynchronousQueue(),
          threadFactory);
 }

超时timeout设置为0的话,表示不等待

public E poll(long timeout, TimeUnit unit) throws InterruptedException {
  return pollFirst(timeout, unit);
 }

具体如下

public E pollFirst(long timeout, TimeUnit unit)
  throws InterruptedException {
  long nanos = unit.toNanos(timeout);
  final ReentrantLock lock = this.lock;
  lock.lockInterruptibly();
  try {
   E x;
   while ( (x = unlinkFirst()) == null) {
    if (nanos <= 0)
     return null;
    nanos = notEmpty.awaitNanos(nanos);
   }
   return x;
  } finally {
   lock.unlock();
  }
 }

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。


推荐阅读
  • 本文介绍如何通过SSH协议使用Xshell远程连接到Ubuntu系统。为了实现这一目标,需要确保Ubuntu系统已安装并配置好SSH服务器,并保证网络连通性。 ... [详细]
  • 深入解析 Spring Security 用户认证机制
    本文将详细介绍 Spring Security 中用户登录认证的核心流程,重点分析 AbstractAuthenticationProcessingFilter 和 AuthenticationManager 的工作原理。通过理解这些组件的实现,读者可以更好地掌握 Spring Security 的认证机制。 ... [详细]
  • 本文将详细介绍如何在没有显示器的情况下,使用Raspberry Pi Imager为树莓派4B安装操作系统,并进行基本配置,包括设置SSH、WiFi连接以及更新软件源。 ... [详细]
  • 本文探讨了如何通过一系列技术手段提升Spring Boot项目的并发处理能力,解决生产环境中因慢请求导致的系统性能下降问题。 ... [详细]
  • 本文详细介绍如何通过设置SSH密钥来获取连接GitHub远程仓库的权限,包括生成密钥、添加到GitHub账户以及验证连接等步骤。 ... [详细]
  • 本文介绍如何配置SecureCRT以正确显示Linux终端的颜色,并解决中文显示问题。通过简单的步骤设置,可以显著提升使用体验。 ... [详细]
  • 最新计算机专业原创毕业设计参考选题都有源码+数据库是近期作品ling取参考你的选题刚好在下面有,有时间看到机会给您发1ssm资源循环利用2springboot校园考勤系统3ssm防 ... [详细]
  • CentOS 7.2 配置防火墙端口开放
    本文介绍如何在 CentOS 7.2 系统上配置防火墙以开放特定的服务端口,包括 FTP 服务的临时与永久开放方法,以及如何验证配置是否生效。 ... [详细]
  • 本指南详细介绍了如何在同一台计算机上配置多个GitHub账户,并使用不同的SSH密钥进行身份验证,确保每个账户的安全性和独立性。 ... [详细]
  • WinSCP: 跨Windows与Linux系统的高效文件传输解决方案
    本文详细介绍了一款名为WinSCP的开源图形化SFTP客户端,该工具支持SSH协议,适用于Windows操作系统,能够实现与Linux系统之间的文件传输。对于从事嵌入式开发的技术人员来说,掌握WinSCP的使用方法将极大提高工作效率。 ... [详细]
  • 利用SSH隧道实现外网对局域网机器的安全访问
    本文探讨了一种常见的网络配置问题及其解决方案,即如何在外网环境下安全地访问位于局域网内的计算机。特别介绍了使用SSH反向隧道技术来实现这一目标的具体步骤和注意事项。 ... [详细]
  • 在Linux系统上构建Web服务器的详细步骤
    本文详细介绍了如何在Linux系统上搭建Web服务器的过程,包括安装Apache、PHP和MySQL等关键组件,以及遇到的一些常见问题及其解决方案。 ... [详细]
  • 本文详细介绍了如何通过Git Bash在本地仓库与远程仓库之间建立连接并进行同步操作,包括克隆仓库、提交更改和推送更新等步骤。 ... [详细]
  • Windows 环境下安装 Git 并连接 GitHub 的详细步骤
    本文详细介绍了如何在 Windows 系统中安装 Git 工具,并通过配置 SSH 密钥实现与 GitHub 的安全连接。包括下载、安装、环境配置及验证连接等关键步骤。 ... [详细]
  • Redis安全防护深入解析
    本文详细探讨了如何通过指令安全、端口管理和SSL代理等措施有效保护Redis服务的安全性。 ... [详细]
author-avatar
僵小鱼
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有