threw out all unnecessary from the code:
@RestController @Scope("prototype") public class Test { private static List<DiOr> deliveryOrderList; private static List<TK> objList; private static ExecutorService executor; public Test() { objList = new ArrayList<TK>(); objList.add(new Obj1()); objList.add(new Obj2()); // неизвестно сколько может быть ... executor = Executors.newFixedThreadPool(objList.size()); } @RequestMapping(value = "/test", method = RequestMethod.GET, produces = "application/json;charset=UTF-8") public synchronized static List<DiOr> getTest() { deliveryOrderList = new ArrayList<>(); objList.stream().forEach((s) -> { final TK finalS = s; executor.submit(() -> { deliveryOrderList.add(finalS.get()); }); }); executor.shutdown(); executor.awaitTermination(2, TimeUnit.MINUTES); return deliveryOrderList; } } In the end, I want to achieve that when accessing URL / test, objects created in the constructor run one method (each method runs at different times) in a separate thread of someone or the pool. after completion of execution of all threads of the pool, the method returned some json (there are no problems here).
Since the returned object is static, if you remove synchronized static - if you simultaneously request to the URL more than once, incorrect data will be returned, some method will work, some will not have time, and so on.
When I finish synchronized static (as in the example) everything is done correctly, but as I understand it, a queue is formed for executing these pools ... and if you simultaneously query this URL, the running time increases significantly, for those who are in this queue . For me, this is critical ...
How best to solve this problem?
finalS.get()what does?forEach()needs to be replaced byparallelStram().map()- Senior Pomidor