I’m trying to create a ThreadPoolExecutor with a certain number of threads, but at the same time, i want to control the size of the pool queue. So I created the executor using the full constructor:
BlockingQueue<Runnable> pq =
new ArrayBlockingQueue<Runnable>(MAX_THREADPOOL_SIZE);
ThreadPoolExecutor threadPoolExecutor =
new ThreadPoolExecutor(threadSize, threadSize, THREAD_IDLE_WAIT,
TimeUnit.SECONDS, pq);
However, this gives me an IllegalArgumentException. If I change the constructor to
new ThreadPoolExecutor(threadSize, **threadSize+1**, THREAD_IDLE_WAIT,
TimeUnit.SECONDS, pq);
it works. Why won’t it work if I want the ideal and max amount of threads to be the same.
From javadoc: if corePoolSize, or keepAliveTime less than zero, or if maximumPoolSize less than or equal to zero, or if corePoolSize greater than maximumPoolSize. So they can also be equal. I have also tried constructing with equal values and it works. Maybe the source code can help you find out what the problem is: