如何将单个线程上的操作分发到不同的多个线程

时间:2013-06-27 11:50:12

标签: multithreading multiprocessing named-pipes python-multithreading

如何分发操作,比如将一个管道中发送的items \ actions复制到可以访问原始管道的各种不同管道?

假设我有父线程是“Pthread”,我想将它链接到4或5个子线程,就像二叉树一样。在“Pthread”上执行的任何操作都应该分发给所有子线程(类似于ESB在SOA架构中所做的操作)。

One to Many in Distribution in Pipe

类似A + B应该同时在所有5个线程\管道中发送并处理。

有办法做到这一点吗?

1 个答案:

答案 0 :(得分:1)

 public class MainThreadEntry  {



     public void ThreadCreationMethod()
        {
         List<Future<Object>> listOfResult = null; // listOfResult is list of Integer objects as a result of computation by different threads
         ExecutorService executor = Executors.newFixedThreadPool(5); // no of threads to create from main thread
         List<EachThreadComputation> list = new ArrayList<MainThreadEntry .EachThreadComputation>();
         for (int i = 0; i < 5; i++) {
             EachThreadComputation separeateComputaionInnerClass = new EachThreadComputation(1,2); // innerClass Created For Ecah Thread 1,2 parameter can be dynamic
             list.add(separeateComputaionInnerClass);
         }
         try {

             listOfResult = executor.invokeAll(list); // call on different threads with 5 separate executionpath for computation

         } catch (InterruptedException e) {

         }






        }


private class EachThreadComputation implements Callable<Object>{
          private int A;
          private int B;


          EachThreadComputation(int A,int B) {
                this.A = A;
                this.B = B;

            }


            @Override
            public Object call() throws Exception {
                   return (Integer)A+B

      }
      }}