ThreadPoolExecutor的maximumPoolSize如何工作? [英] How does maximumPoolSize of ThreadPoolExecutor works?

查看:343
本文介绍了ThreadPoolExecutor的maximumPoolSize如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试了解ThreadPoolExecutor类.我已经阅读了 answer 和Javadoc.但是我的实验与该描述不符:

I'm trying to understand ThreadPoolExecutor class. I have read this answer and the Javadoc. But my experimentation doesn't match with that description:

我使用工厂来初始化线程池以跟踪ID

I initialize the threadpool with an factory for tracking the ids

int tcounter = 0;
ThreadPoolExecutor executor = new ThreadPoolExecutor(2, 4, 1, TimeUnit.MINUTES,
        new ArrayBlockingQueue<Runnable>(1000), new ThreadFactory() {

            @Override
            public Thread newThread(Runnable r) {
                return new mThread(tcounter++, r);
            }
        });

public class mThread extends Thread {
    int id;

    private mThread(int id, Runnable run) {
        super(run);
        GLog.e("created thread " + id);
        this.id = id;
    }

}

然后执行任务:

public class mRunanble implements Runnable {
    int value = 0;

    private mRunanble(int value) {
        super();
        this.value = value;
    }

    @Override
    public void run() {
        SystemClock.sleep(3000);
        Thread t = Thread.currentThread();
        if (t instanceof mThread) {

            GLog.e("Say " + (value) + " on thread " + ((mThread) t).id);
        } 

    }

}

并为按钮分配操作:

executor.execute(new mRunanble(i++));

但是我不会向该按钮发送垃圾邮件,并且永远不会创建第三个线程,因此ThreadPoolExecutor构造函数(maximumPoolSize=4)中的第二个参数是什么. 我希望在执行结束1分钟后创建4个线程,并杀死其中2个线程

But I spam that button and the third thread is never created, so what is for the second paramether in the ThreadPoolExecutor constructor (maximumPoolSize=4). I was specting 4 threads to be created and 2 of them to be killed after 1 minute of the end of the execution

推荐答案

在ThreadPoolExecutor中,当corePoolSize no不足以执行您的任务时,maximumPoolSize出现,并且如果所有no都被任务占用,则仅创建一个踏步执行任务.此no不能增长到maxPoolSize.

In ThreadPoolExecutor maximumPoolSize comes in picture when corePoolSize no is not sufficient to execute your tasks and if all no are ocupied by tasks then only one more tread is being created to execute task .this no can grow upto maxPoolSize.

编辑,您误解了maxPoolsize概念.请参考下面的链接.

Edit you missunderstood the maxPoolsize concept.please refer below link.

http://www.bigsoft.co.uk/blog/index.php/2009/11/27/rules-of-a-threadpoolexecutor-pool-size

这篇关于ThreadPoolExecutor的maximumPoolSize如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆