All solutions for parallel work are associated with the emergence of new threads or sending jobs to the thread pool for remote network calls.
A good way to avoid thread safety issues is to use the executorService and subclass Callable<T> (to submit(Callable) or invokeAll(Collection<Callable>) ), and Callables will return a response value. That way, your original method can simply handle the return values โโof each call and choose to set responses in the session or update other objects, rather than this work happening in another thread.
So, the basic algorithm:
- Send each of these calls to the executor
Callable<T> in Callable<T> subclasses - Build the
Future<T> , which you will return from the executorService function - Call
Future.get() for each block until you get a response, and then process the answers, but you want to return to the main thread
source share