9

我正在尝试了解 ThreadPoolExecutor 类。我已阅读此答案和 Javadoc。但我的实验与该描述不符:

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

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) 中的第二个参数是什么。我预计要创建 4 个线程,其中 2 个线程在执行结束 1 分钟后被杀死

4

3 回答 3

5

来自 API ThreadPoolExecutor

如果运行的线程数多于 corePoolSize 但少于 maximumPoolSize,则仅当队列已满时才会创建新线程。

您的队列永远不会填满,因为它的容量为1000. 如果将容量更改为1,您将看到Threads 正在创建。

该类Executors使用 aSynchronousQueue作为其newCachedThreadPool方法,因此您可能也需要考虑使用它。

于 2012-09-02T14:42:33.343 回答
2

在 ThreadPoolExecutor 中,maxPoolSize 出现在 corePoolSize no 不足以执行您的任务并且如果所有 no 都被任务占用,那么只会再创建一个线程来执行任务。这个 no 可以增长到 maxPoolSize。

编辑您误解了 maxPoolsize 概念。请参阅下面的链接。

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

于 2012-09-02T14:31:27.843 回答
1

要使 ThreadPool 创建新的附加线程(根据maximumPoolSize参数扩展池大小),请尝试执行以下简单示例:

public class Main {

    public static void main(String[] args) throws InterruptedException {

        ThreadPoolExecutor tpe = new ThreadPoolExecutor(
                1, 2, 500, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<>(1));
        System.out.println("init pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size());

        tpe.execute(new Task("1st", 10000));
        Thread.sleep(1000);
        print(tpe, "1st");

        tpe.execute(new Task("2nd", 0));
        Thread.sleep(1000);
        print(tpe, "2nd");

        tpe.execute(new Task("3d", 2000));
        Thread.sleep(1000);
        print(tpe, "3d");

        while (tpe.getPoolSize()>1) {           
            Thread.sleep(100);
        }
        System.out.println("pool size= " + tpe.getPoolSize() + ", queue size=" + tpe.getQueue().size());
        tpe.shutdown();
    }

    private static void print(ThreadPoolExecutor tpe, String name) {
        System.out.println("After " + name + " execute -  pool size= " + tpe.getPoolSize() + ", queue=" + tpe.getQueue());
    }

    private static class Task implements Runnable {

        private final String name;
        private final long time;

        Task(String name, long time) {
            this.name = name;
            this.time = time;
        }

        @Override
        public void run() {
            System.out.println("Run " + Thread.currentThread().getName() + "-" + name);
            try {
                Thread.sleep(time);
            } catch (InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            System.out.println("Finish " + Thread.currentThread().getName() + "-" + name);
        }

        @Override
        public String toString() {
            return name;
        }

    }
}

您将获得展示maximumPoolSizekeepAliveTime影响的输出:

init pool size= 0, queue size=0
Run pool-1-thread-1-1st
After 1st execute -  pool size= 1, queue=[]
After 2nd execute -  pool size= 1, queue=[2nd]
Run pool-1-thread-2-3d
After 3d execute -  pool size= 2, queue=[2nd]
Finish pool-1-thread-2-3d
Run pool-1-thread-2-2nd
Finish pool-1-thread-2-2nd
pool size= 1, queue size=0
Finish pool-1-thread-1-1st
于 2016-07-27T14:29:11.490 回答