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