Multithreading 如何将单个线程上的操作分发到不同的多个线程
如何将操作(例如复制在一个管道中发送的项目\操作)分发到可以访问原始管道的各种不同管道 假设我的父线程是“Pthread”,我想将它链接到4或5个子线程,就像二叉树一样。在“Pthread”上执行的任何操作都应该分发到所有子线程(类似于ESB在SOA体系结构中所做的事情) 像A+B应该同时在所有5个线程\管道中发送并处理 有办法做到这一点吗?Multithreading 如何将单个线程上的操作分发到不同的多个线程,multithreading,multiprocessing,named-pipes,python-multithreading,Multithreading,Multiprocessing,Named Pipes,Python Multithreading,如何将操作(例如复制在一个管道中发送的项目\操作)分发到可以访问原始管道的各种不同管道 假设我的父线程是“Pthread”,我想将它链接到4或5个子线程,就像二叉树一样。在“Pthread”上执行的任何操作都应该分发到所有子线程(类似于ESB在SOA体系结构中所做的事情) 像A+B应该同时在所有5个线程\管道中发送并处理 有办法做到这一点吗?公共类MainThreadEntry{ public class MainThreadEntry { public void Thre
公共类MainThreadEntry{
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
}
}}
public void ThreadCreationMethod()
{
List listOfResult=null;//listOfResult是不同线程计算结果的整数对象列表
ExecutorService executor=Executors.newFixedThreadPool(5);//要从主线程创建的线程数
列表=新的ArrayList();
对于(int i=0;i<5;i++){
EachThreadComputation separeatecomputationinnerclass=neweachthreadcomputation(1,2);//为Ecah线程1,2参数创建的内部类可以是动态的
list.add(separeatecomputeioninnerclass);
}
试一试{
listOfResult=executor.invokeAll(list);//使用5个单独的executionpath调用不同的线程进行计算
}捕捉(中断异常e){
}
}
私有类eachThreadComputement实现可调用{
私人INTA;
私人INTB;
每个线程计算(inta,intb){
这个A=A;
这个.B=B;
}
@凌驾
公共对象调用()引发异常{
返回(整数)A+B
}
}}