0

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

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

管道中的一对多分配

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

有没有办法做到这一点?

4

1 回答 1

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

      }
      }}
于 2013-06-27T12:22:40.143 回答