我需要启动一个可变数目的线程继而每个开始变化的数量的线程(即我的线程,其中第i个线程需要开始起的线程)中的弹簧的应用。
假设每个“我线程”的包含该自动装配我将如何产生这些实例的内部类? 所以我有一个A bean
需要以某种方式生成一个bean的实例,需要进行spring管理以满足其依赖关系。Spring和多线程
我写的是什么,我认为是我的解决方案的基础很短的示例代码和我打上代码我不知道如何???写:
@Component
public class MasterOrchestrator {
public void do(List<DataObjWrapper> list){
ExecutorService es = Executors.newFixedThreadPool(list.size());
for (DataObjWrapper dataObjWrapper : list){
es.submit(???);
}
}
}
@Component
public class ThreadWorkerI implements Runnable{
private int numThreadsForMessageType;
private int numRunsForMessageType;
private DataObj dataObj;
public ThreadWorkerI(int numThreadsForMessageType, int numRunsForMessageType, DataObj dataObj){
this.numThreadsForMessageType = numThreadsForMessageType;
this.numRunsForMessageType = numRunsForMessageType;
this.dataObj = dataObj;
}
@Autowired
private JmsTemplate jmsTemplate;
public void run(){
ExecutorService es = Executors.newFixedThreadPool(numThreadsForMessageType);
for (int i=0;i<numRunsForMessageType;i++){
es.submit(new ActualWorker(i));
}
}
private class ActualWorker implements Runnable{
private int numRun;
private ActualWorker(int numRun){
this.numRun = numRun;
}
public void run(){
//send message using the jmsTemplate the dataObj and numRun
}
}
}
DatObjWrapper
包含除其他成员numThreadsForMessageType
,numRunsForMessageType
和dataObj
。
我去了你给的链接在这里(前),我不知道这是如何能帮助我。请注意,在这个例子中,他们将自己实例化为'MessagePrinterTask'实例,这意味着不存在弹簧管理的bean。如果“I threads”以某种方式被spring管理的bean,那么他们可以自动装配我需要的东西,并且他们将实例化一个可以访问其自动装配资源的内部类。问题在于第一个链接。我可以只做context.getBean,但我宁愿不要。 – Ittai