Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does maximumPoolSize of ThreadPoolExecutor works?

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

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

}

then the task:

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);
        } 

    }

}

and assign a button the action:

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

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

like image 763
Addev Avatar asked Sep 02 '12 14:09

Addev


People also ask

How does thread pool executor works?

ThreadPoolExecutor is an ExecutorService to execute each submitted task using one of possibly several pooled threads, normally configured using Executors factory methods. It also provides various utility methods to check current threads statistics and control them.

How does thread pool keeps the thread alive?

Instead of creating new threads when new tasks arrive, a thread pool keeps a number of idle threads that are ready for executing tasks as needed. After a thread completes execution of a task, it does not die. Instead it remains idle in the pool waiting to be chosen for executing new tasks.

What is corePoolSize and max pool size in ThreadPoolExecutor?

Starting thread pool size is 1, core pool size is 5, max pool size is 10 and the queue is 100. As requests come in, threads will be created up to 5 and then tasks will be added to the queue until it reaches 100.

What is keepAliveTime in ThreadPoolExecutor?

keepAliveTime - when the number of threads is greater than the core, this is the maximum time that excess idle threads will wait for new tasks before terminating. unit - the time unit for the keepAliveTime argument. workQueue - the queue to use for holding tasks before the are executed.


2 Answers

From the API for ThreadPoolExecutor:

If there are more than corePoolSize but less than maximumPoolSize threads running, a new thread will be created only if the queue is full.

Your queue never fills since it has a capacity of 1000. If you change the capacity to 1, you will see Threads being created.

The Executors class uses a SynchronousQueue for its newCachedThreadPool methods, so you might want to consider using it as well.

like image 177
Jeffrey Avatar answered Oct 19 '22 23:10

Jeffrey


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.

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

like image 40
amicngh Avatar answered Oct 19 '22 23:10

amicngh