0
让我明确的议程第一:共享的ApplicationContext与执行人
- 我有1000个请求数据。
- 我会读取所有1000个请求,并且我会将1000请求提交给执行者。
- 每个任务都会碰到soap web服务并获得响应。
问:
- 我有共同的,这将是相同的所有线程应用程序上下文。
- 在bean.xml文件中,我有我想用来创建肥皂请求的protoype 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();
}
}
在我的Runnable中,我将使用prototype bean打击web服务。 我所有的任务都是从相同的应用程序上下文获取原型bean 所以我的同一个应用程序上下文会导致任何问题? 请帮忙 代码和我上面提到的一样 –
通过不同的线程从相同的应用程序上下文获取工作bean没有问题。但是,如果你的工作者bean共享/修改相同的变量/引用,那么就存在一个问题。 – isah
你是什么意思分享相同的变量,我想你的意思是共享同一个应用程序contex singleton bean? 所以如果它的原型bean服务于应用程序上下文,所以我没有任何问题 –