I am new to concurrency and threading in java and I have this following scenario -
Function1(fetches next task from a queue and calls function2 with task) then Function2(calls Function Run to run this task) then Run(function run is submiting task using executor service which has a fixed thread pool and code is like below)
ExecutorService exeService = Executors.newFixedThreadPool(3);
private void run(Task task){
Abstract batchExecutor = getBatchExecutor(param1,param2, param3, param4, task);
Future<Void> future = exeService.submit(batchExecutor);
while (!future.isDone()) {
if (isStopRequested.get()) {
try {
future.get(5, TimeUnit.MINUTES);
} catch (TimeoutException e) {
e.printStackTrace();
}
throw new InterruptedException("message");
}
Thread.sleep(3000);
}
future.get();
Map<String, Result> submittedObjects = batchExecutor.getSubmittedObjects();
storeSubmittedObjects(submittedObjects);
}
My problem is even after declaring a thread pool also tasks are still running in sequential way. How can I make it parallel so that as soon as there is another call to the run it should use free thread from pool and submit the task?
There is no return type for all these functions.