I am a beginner with ThreadPoolExecutor and need your expert advice for below scenario
Here,I submit 30 jobs in a thread executor which should get executed in sequence.
ThreadPoolExecutor exeSer = new ThreadPoolExecutor(5, 10, 15, TimeUnit.SECONDS,new ArrayBlockingQueue<Runnable>(20));
for(int i = 0; i<30; i++) {
exeSer.submit(new MyRunnableService(i));
}
Current Behaviour
When I submit 30 jobs to execute, the executor created 5 threads for first 5 jobs and start execution. The next 20 jobs goes into Queue and then it executes remaining 5 jobs. So first 5 (1-5) and last 5 (26-30) jobs get executed and other (6-25) jobs goes into queue.
Expectation
The expected behaviour is that, first 5(1-5) should get executed as per corePoolSize and next 5 (6-10) should also get executed as per maxPoolSize and remaining 20 (11-30) should go into the queue.
Problem
I want to execute the job in a sequence but the problem is that last 5 (26-30) jobs executed first.
Is there any way I can change the behaviour as expected or any other Executor implementation avialable who meets the requirement?
Please guide.