I have an app that allows users to batch add watermarks to images. The app would only use one thread and can only add one watermark at a time.
I want the user to be able to change the number of watermark tasks[threads] running at a time: maybe [1-5] in settings, and I can't use a fixedThreadPool, as it has a fixed pool size.
I looked into using a ThreadPoolExecutor private static ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); and each time the user changes the number of threads I invoke the ThreadPoolExecutor.setMaxPoolSize(newMaxThreadCount).
Now when I try to submit say 15 image watermark tasks to the executor with a maximum pool size of 3, I get the following exception:
java.util.concurrent.RejectedExecutionException: Task com.darkmental.zeondownloader.app.main.phototools.watermark.BulkWatermarkTask$$Lambda$710/1861585081@1e2d8ad4 rejected from java.util.concurrent.ThreadPoolExecutor@24d02747[Running, pool size = 3, active threads = 3, queued tasks = 0, completed tasks = 0]
I expected the ThreadPoolExecutor to have the same behavior as a fixedThreadPool where I can have a pool size of 10 and still submit as many tasks as I want, but I didn't. How can I do that?
ThreadPoolExecutor, you should usenew ThreadPoolExecutor(…)and not abuse a factory method that intentionally specifies a more abstract return type. - Holger