java thread属性_Java源码解析线程池ThreadPoolExecutor的属性

版权声明:博主保留一切权利,转载请注明出处。https://blog.csdn.net/li_canhui/article/details/85696201

线程池ThreadPoolExecutor的简单介绍,可以参考这篇文章https://blog.csdn.net/li_canhui/article/details/85088675。本文主要介绍一下ThreadPoolExecutor中的主要属性。

首先看一下ctl,代码如下。ctl是主要的线程池控制状态,它是一个原子整数,在里面打包了两个概念上的域:

1,workerCount, 表示活动的线程数量。

2,runState,表示是否正在运行,关闭等。

线程池使用了一个32位的整数,来表示线程数量和线程池的运行状态。其中该整数ctl的第29位表示线程数量,高3位表示线程池的运行状态。

[code] /**

* The main pool control state, ctl, is an atomic integer packing

* two conceptual fields

* workerCount, indicating the effective number of threads

* runState, indicating whether running, shutting down etc

*

* In order to pack them into one int, we limit workerCount to

* (2^29)-1 (about 500 million) threads rather than (2^31)-1 (2

* billion) otherwise representable. If this is ever an issue in

* the future, the variable can be changed to be an AtomicLong,

* and the shift/mask constants below adjusted. But until the need

* arises, this code is a bit faster and simpler using an int.

*

* The workerCount is the number of workers that have been

* permitted to start and not permitted to stop. The value may be

* transiently different from the actual number of live threads,

* for example when a ThreadFactory fails to create a thread when

* asked, and when exiting threads are still performing

* bookkeeping before terminating. The user-visible pool size is

* reported as the current size of the workers set.

*

* The runState provides the main lifecycle control, taking on values:

*

* RUNNING: Accept new tasks and process queued tasks

* SHUTDOWN: Don't accept new tasks, but process queued tasks

* STOP: Don't accept new tasks, don't process queued tasks,

* and interrupt in-progress tasks

* TIDYING: All tasks have terminated, workerCount is zero,

* the thread transitioning to state TIDYING

* will run the terminated() hook method

* TERMINATED: terminated() has completed

*

* The numerical order among these values matters, to allow

* ordered comparisons. The runState monotonically increases over

* time, but need not hit each state. The transitions are:

*

* RUNNING -> SHUTDOWN

* On invocation of shutdown(), perhaps implicitly in finalize()

* (RUNNING or SHUTDOWN) -> STOP

* On invocation of shutdownNow()

* SHUTDOWN -> TIDYING

* When both queue and pool are empty

* STOP -> TIDYING

* When pool is empty

* TIDYING -> TERMINATED

* When the terminated() hook method has completed

*

* Threads waiting in awaitTermination() will return when the

* state reaches TERMINATED.

*

* Detecting the transition from SHUTDOWN to TIDYING is less

* straightforward than you'd like because the queue may become

* empty after non-empty and vice versa during SHUTDOWN state, but

* we can only terminate if, after seeing that it is empty, we see

* that workerCount is 0 (which sometimes entails a recheck -- see

* below).

*/

private final AtomicInteger ctl = new AtomicInteger(ctlOf(RUNNING, 0));

接下来看一下workerQueue,如下图。workerQueue是用来存放任务并分发给工作线程的。然后是mainLock和termination条件,用来控制线程访问。workers是一个HashSet,用来保存所有的工作线程。largestPoolSize记录已经获得的最大的线程池大小,completedTaskCount,表示已经完成的任务个数。

[code] /**

* The queue used for holding tasks and handing off to worker

* threads. We do not require that workQueue.poll() returning

* null necessarily means that workQueue.isEmpty(), so rely

* solely on isEmpty to see if the queue is empty (which we must

* do for example when deciding whether to transition from

* SHUTDOWN to TIDYING). This accommodates special-purpose

* queues such as DelayQueues for which poll() is allowed to

* return null even if it may later return non-null when delays

* expire.

*/

private final BlockingQueue workQueue;

/**

* Lock held on access to workers set and related bookkeeping.

* While we could use a concurrent set of some sort, it turns out

* to be generally preferable to use a lock. Among the reasons is

* that this serializes interruptIdleWorkers, which avoids

* unnecessary interrupt storms, especially during shutdown.

* Otherwise exiting threads would concurrently interrupt those

* that have not yet interrupted. It also simplifies some of the

* associated statistics bookkeeping of largestPoolSize etc. We

* also hold mainLock on shutdown and shutdownNow, for the sake of

* ensuring workers set is stable while separately checking

* permission to interrupt and actually interrupting.

*/

private final ReentrantLock mainLock = new ReentrantLock();

/**

* Set containing all worker threads in pool. Accessed only when

* holding mainLock.

*/

private final HashSet workers = new HashSet();

/**

* Wait condition to support awaitTermination

*/

private final Condition termination = mainLock.newCondition();

/**

* Tracks largest attained pool size. Accessed only under

* mainLock.

*/

private int largestPoolSize;

/**

* Counter for completed tasks. Updated only on termination of

* worker threads. Accessed only under mainLock.

*/

private long completedTaskCount;

接下来是用户相关的参数。所有的用户控制的参数都是用volatile声明的,这样各个动作就可以基于它们的最新的值了,并且不需要用锁,因为没有任何依赖于它们的内部不变性需要和别的动作同步修改。这部分的主要参数如下。

[code] /**

* Factory for new threads. All threads are created using this

* factory (via method addWorker). All callers must be prepared

* for addWorker to fail, which may reflect a system or user's

* policy limiting the number of threads. Even though it is not

* treated as an error, failure to create threads may result in

* new tasks being rejected or existing ones remaining stuck in

* the queue.

*

* We go further and preserve pool invariants even in the face of

* errors such as OutOfMemoryError, that might be thrown while

* trying to create threads. Such errors are rather common due to

* the need to allocate a native stack in Thread.start, and users

* will want to perform clean pool shutdown to clean up. There

* will likely be enough memory available for the cleanup code to

* complete without encountering yet another OutOfMemoryError.

*/

private volatile ThreadFactory threadFactory;

/**

* Handler called when saturated or shutdown in execute.

*/

private volatile RejectedExecutionHandler handler;

/**

* 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;

/**

* 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.

*/

private volatile int corePoolSize;

/**

* Maximum pool size. Note that the actual maximum is internally

* bounded by CAPACITY.

*/

private volatile int maximumPoolSize;

/**

* The default rejected execution handler

*/

private static final RejectedExecutionHandler defaultHandler =

new AbortPolicy();

/**

* Permission required for callers of shutdown and shutdownNow.

* We additionally require (see checkShutdownAccess) that callers

* have permission to actually interrupt threads in the worker set

* (as governed by Thread.interrupt, which relies on

* ThreadGroup.checkAccess, which in turn relies on

* SecurityManager.checkAccess). Shutdowns are attempted only if

* these checks pass.

*

* All actual invocations of Thread.interrupt (see

* interruptIdleWorkers and interruptWorkers) ignore

* SecurityExceptions, meaning that the attempted interrupts

* silently fail. In the case of shutdown, they should not fail

* unless the SecurityManager has inconsistent policies, sometimes

* allowing access to a thread and sometimes not. In such cases,

* failure to actually interrupt threads may disable or delay full

* termination. Other uses of interruptIdleWorkers are advisory,

* and failure to actually interrupt will merely delay response to

* configuration changes so is not handled exceptionally.

*/

private static final RuntimePermission shutdownPerm =

new RuntimePermission("modifyThread");

/* The context to be used when executing the finalizer, or null. */

private final AccessControlContext acc;

[code]

threadFactory用于创建线程。当线程池饱和或者关闭之后,会调用RejectedExecutionHandler handler。keepAliveTime,allowCoreThreadTimeOut,corePoolSize,maximumPoolSize是线程池的几个核心参数,这里就不在赘述了,详细可以参见https://blog.csdn.net/li_canhui/article/details/85088675。defaultHandler是默认拒绝策略。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值