Search code examples
javaspringmultithreadingexecutorservicefuturetask

Sharing Object across threads in java which calls AsynFuture


I have created a runnable class A which executes some task for me. I am submitting this class using ExecutorService so that I can execute these tasks in parallel.

The runnable class A calls another object B which sends an AsyncFuture request (future.get() one).

I am submitting separate objects of runnable class A to the ExecutorService, however, the object of class B is being referred by a bean (singleton). Can this cause issues in thread execution?

I am noticing that some of the objects of class A are not being executed by any thread.

I read something about using ThreadLocal but I am not sure if it's applicable here.


Solution

  • Referring to a singleton bean from different objects, albeit across separate threads does cause a bottleneck. The only solution that worked for me was to lookup this bean on A's initialization.