Spring和多线程

发布于 2024-12-03 20:02:17 字数 1707 浏览 2 评论 0原文

我需要在 Spring 应用程序中启动可变数量的线程,每个线程又启动不同数量的线程(即,第 I 个线程需要启动 Ki 线程的 i 个线程)。
假设每个“I 线程”都包含一个自动装配的内部类,我将如何生成这些实例? 所以我有一个 A bean ,它需要以某种方式生成一个 I 的 bean 实例,该实例需要由 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 包含其他成员numThreadsForMessageTypenumRunsForMessageTypedataObj

I need to start a variable number of threads which in turn each start a varying number of threads (i.e. i threads where the Ith thread needs to start Ki threads) in a spring application.
assuming each of the "I threads" contains an inner class which is autowired how will I generate those instances?
So I have an A bean which needs to somehow generate I instances of a bean which needs to be spring managed to satisfy its dependencies.

I've written a short sample code of what I think is the base for my solution and I've marked the code I'm not sure how to write by ???:

@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 contains amongst other members the numThreadsForMessageType, numRunsForMessageType and dataObj.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

脸赞 2024-12-10 20:02:17

您可以使用 @Configurable 注释让 Spring 将依赖项注入到您的工作线程中 - 即使那些不是由 Spring 容器显式管理的。

You can use @Configurable annotation to let Spring inject dependencies into your workers - even the one's that are not managed explicitly by the Spring container.

じее 2024-12-10 20:02:17

与其启动自己的线程,不如使用线程池或 Spring 的 任务执行器抽象。然后您的任务可以是 Spring bean 或手动实例化。

Instead of starting your own threads it is better to use thread pool or Spring's task executor abstraction. Then your tasks can be Spring beans or manually instantiated.

痴者 2024-12-10 20:02:17

将 MasterOrchestrator 或 ThreadWorker 的引用传递给 Runnable 的构造函数怎么样?然后你可以将所有配置放入@Component类中。

例如:

 private class ActualWorker implements Runnable{
      private int numRun;
      private ActualWorker(ThreadWorkerI owner, int numRun){
          this.numRun = numRun;
      }
      public void run(){
          //send message using owner.jmsTemplate, owner.dataObj and numRun
      }
  }

How about passing a reference to the MasterOrchestrator or the ThreadWorker into the Runnable's constructor? Then you can put all the configuration into the @Component class.

For example:

 private class ActualWorker implements Runnable{
      private int numRun;
      private ActualWorker(ThreadWorkerI owner, int numRun){
          this.numRun = numRun;
      }
      public void run(){
          //send message using owner.jmsTemplate, owner.dataObj and numRun
      }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文