简体   繁体   English

在调用AsynFuture的Java中的线程之间共享对象

[英]Sharing Object across threads in java which calls AsynFuture

I have created a runnable class A which executes some task for me. 我创建了一个可运行的类A,它为我执行一些任务。 I am submitting this class using ExecutorService so that I can execute these tasks in parallel. 我正在使用ExecutorService提交此类,以便可以并行执行这些任务。

The runnable class A calls another object B which sends an AsyncFuture request (future.get() one). 可运行类A调用另一个对象B,该对象发送AsyncFuture请求(future.get()一个)。

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). 我正在将可运行类A的单独对象提交给ExecutorService,但是,类B(单个)正在引用类B的对象。 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. 我注意到,任何线程都没有执行A类的某些对象。

I read something about using ThreadLocal but I am not sure if it's applicable here. 我读了一些有关使用ThreadLocal的内容,但不确定在这里是否适用。

Referring to a singleton bean from different objects, albeit across separate threads does cause a bottleneck. 引用来自不同对象的单例bean,尽管跨越单独的线程确实会造成瓶颈。 The only solution that worked for me was to lookup this bean on A's initialization. 对我有用的唯一解决方案是在A的初始化中查找此bean。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM