首先让我清楚议程:
问题:
以下是示例代码:
import java.io.ObjectInputStream.GetField;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class AppContext
{
ApplicationContext sharedContext = new ClassPathXmlApplicationContext("Beans.xml");
public static ApplicationContext getAppContext()
{
if(sharedContext!=null)
return sharedContext; //will this cause any isseu while accessing by multiple threads
}
}
public class Testing {
public static void main(String args[])
{
//here I tried to submit the task using ExecutorService and want to use the same application context
//can I pass the prototypeBean in all the task with out synchronization issue?
//because My appcontext is static so will it cause any issue while accessing my multiple threads
ExecutorService service=Executors.newFixedThreadPool(10);
service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean")));
service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean")));
service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean")));
service.submit(new LoopTaskA(AppContext.getAppContext().getBean("myProtoTypeBean")));
service.shutdown();
}
}
答案 0 :(得分:0)
这取决于您Runnables
的行为。如果它们是无状态bean并且不与其他Runnable
共享/修改相同的变量/引用,那么通常你是安全的。如果bean范围是 prototype ,getBean()
将返回一个新实例。
小心池大小,确保设置合理的池大小(参见this)。此外,请确保设置Web服务请求的工作线程设置了适当的超时。