码迷,mamicode.com
首页 > 编程语言 > 详细

【线程池】toString

时间:2020-11-21 12:12:44      阅读:8      评论:0      收藏:0      [点我收藏+]

标签:nstat   ica   loading   super   哪些   less   complete   one   nts   

java.util.concurrent.RejectedExecutionException: Task com.personal.practice.jvm.Jstacktest$1@7d605a5a rejected from java.util.concurrent.ThreadPoolExecutor@722c41f4[Running, pool size = 20, active threads = 20, queued tasks = 10, completed tasks = 10]
	at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2063)
	at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:830)
	at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1379)

 一般我们常见的线程池异常如上所示。

看一下这段日志的输出参数是怎么构成的:

 

技术图片

 

进一步的我们检查ThreadPoolExecutor的toString:

 /**
     * Returns a string identifying this pool, as well as its state,
     * including indications of run state and estimated worker and
     * task counts.
     *
     * @return a string identifying this pool, as well as its state
     */
    public String toString() {
        long ncompleted;
        int nworkers, nactive;
        final ReentrantLock mainLock = this.mainLock;
        mainLock.lock();
        try {
            ncompleted = completedTaskCount;
            nactive = 0;
            nworkers = workers.size();
            for (Worker w : workers) {
                ncompleted += w.completedTasks;
                if (w.isLocked())
                    ++nactive;
            }
        } finally {
            mainLock.unlock();
        }
        int c = ctl.get();
        String rs = (runStateLessThan(c, SHUTDOWN) ? "Running" :
                     (runStateAtLeast(c, TERMINATED) ? "Terminated" :
                      "Shutting down"));
        return super.toString() +
            "[" + rs +
            ", pool size = " + nworkers +
            ", active threads = " + nactive +
            ", queued tasks = " + workQueue.size() +
            ", completed tasks = " + ncompleted +
            "]";
    }

 我们从这里的日志入手看一下各个参数分别对应哪些情况:

1.首先是active,这里有个比较关键的参数是Worker里面的state,为!=0(lock)的时候才被认为是active的:

对应到代码是处于这些状态的worker:

技术图片

 

2.其次是workerqueue和workers的区别,wokerquue就是我们在构造线程池的时候传入的参数,workers指不在队列的那些任务对应一个线程但不一定是active的。

 pool size取的就是workers。

3.queued task直接就是wokerqueue的大小

4.completed tasks是每结束一个任务都会增加的

出现active线程数小于core的原因:

 

【线程池】toString

标签:nstat   ica   loading   super   哪些   less   complete   one   nts   

原文地址:https://www.cnblogs.com/lccsblog/p/13994364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!