如何在Java中使用执行器执行返回不同对象的方法
我有4个方法返回不同类的列表。比方说如何在Java中使用执行器执行返回不同对象的方法,java,executorservice,Java,Executorservice,我有4个方法返回不同类的列表。比方说 public List<A> getA(String param1, String param2){ //some code here } public List<B> getB(String param1, String param2){ //some code here } public List<C> getC(String param1, String param2){ //some code here }
public List<A> getA(String param1, String param2){
//some code here
}
public List<B> getB(String param1, String param2){
//some code here
}
public List<C> getC(String param1, String param2){
//some code here
}
public List<D> getD(String param1, String param2){
//some code here
}
public List getA(字符串param1,字符串param2){
//这里有一些代码
}
公共列表getB(字符串参数1,字符串参数2){
//这里有一些代码
}
公共列表getC(字符串参数1,字符串参数2){
//这里有一些代码
}
公共列表getD(字符串param1,字符串param2){
//这里有一些代码
}
我想同时执行这4个方法,我使用Callable和Executor,如下所示
Callable<List<A>> collableA = new Callable<List<A>>() {
@Override
public List<A> call() throws Exception {
return getA();
}
};
Callable<List<B>> collableB = new Callable<List<B>>() {
@Override
public List<B> call() throws Exception {
return getB();
}
};
Callable<List<D>> collableD = new Callable<List<D>>() {
@Override
public List<D> call() throws Exception {
return getD();
}
};
Callable<List<C>> collableC = new Callable<List<C>>() {
@Override
public List<C> call() throws Exception {
return getC();
}
};
// add to a list
List<Callable> taskList = new ArrayList<Callable>();
taskList.add(collableA );
taskList.add(collableB);
taskList.add(collableC);
taskList.add(collableD);
//create a pool executor with 4 threads
ExecutorService executor = Executors.newFixedThreadPool(4);
Future<List<FrozenFrameAlert>> futureFrozen = executor.submit(taskList);
// getting error here submit() not accepting taskList
Callable collableA=new Callable(){
@凌驾
公共列表调用()引发异常{
返回getA();
}
};
Callable collableB=新的Callable(){
@凌驾
公共列表调用()引发异常{
返回getB();
}
};
Callable collableD=new Callable(){
@凌驾
公共列表调用()引发异常{
返回getD();
}
};
Callable collableC=new Callable(){
@凌驾
公共列表调用()引发异常{
返回getC();
}
};
//添加到列表中
List taskList=new ArrayList();
任务列表。添加(协作);
任务列表。添加(collableB);
任务列表。添加(collableC);
任务列表。添加(协作);
//创建一个包含4个线程的池执行器
ExecutorService executor=Executors.newFixedThreadPool(4);
Future FutureFrozed=执行者提交(任务列表);
//此处获取错误提交()不接受任务列表
我尝试了
InvokeAll()
和invokeAny()
方法,但没有任何方法接受此taskList
我找到了解决方案,您只需对ExecutorService\InvokeAll(Collection>future:futureList)使用通用接口Callable
,而不是Callable
{
System.out.println(future.get());
}
如果您确实有不同的返回类型,您仍然需要将
Callable
与适当的类型一起使用,例如超级接口或对象,至少是这样。错误是您的
List<Callable> taskList = new ArrayList<Callable>();
List taskList=new ArrayList();
不是一般的,应该是这样的
List<Callable<List>> taskList = new ArrayList<>();
List taskList=new ArrayList();
只需对每个可调用项使用submit()
;这将给你四种未来,每种类型都是正确的:
Future<List<A>> futureA = executor.submit(callableA);
Future<List<B>> futureB = executor.submit(callableB);
etc.
Future futureA=执行人提交(callableA);
未来b=执行人提交(可赎回b);
等
如果您在继续之前需要所有四个期货的结果,您可以依次阻止每一个期货:
List<A> resultA = futureA.get();
List<B> resultB = futureB.get();
etc.
List resultA=futureA.get();
List resultB=futureB.get();
等
要做更一般化的事情,您需要找出一种方式,使所有这些列表“相同”。但是,如果您不以相同的方式使用它们,那么它们的类型不同并不重要。invokeAll(collection如果我使用invokeAll()
获取此错误方法invokeAll(collection否,它不适用于我,我在您分段时创建了taskList
。List taskList=new arrarylist()
但是我在向列表中添加对象时遇到了以下错误,如任务列表.add(协作);
类型列表中的add(Callable)方法不适用于参数(Callable)
它应该仍然可以使用列表
,但我刚刚用列表进行了测试
List<A> resultA = futureA.get();
List<B> resultB = futureB.get();
etc.