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