【发布时间】:2017-08-01 08:13:40
【问题描述】:
让我先明确议程:
- 我有 1000 个请求数据。
- 我将阅读所有 1000 请求,并将 1000 请求提交给执行者。
- 每个任务都会访问 soap web 服务并获得响应。
问题:
- 我已共享应用程序上下文,所有线程都相同。
- 在 bean.xml 文件中,我有原型 bean,我想用它来发出肥皂请求。
- 如果我使用共享应用程序上下文并获取 proptype bean,那么它是否会导致共享应用程序上下文变量出现任何同步问题。
下面是示例代码:
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();
}
}
【问题讨论】:
标签: java spring soap executorservice