Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Java中使用执行器执行返回不同对象的方法_Java_Executorservice - Fatal编程技术网

如何在Java中使用执行器执行返回不同对象的方法

如何在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 }

我有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<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.