简体   繁体   English

ExecutorCompletionService不适用于给定参数

[英]ExecutorCompletionService is not applicable for the given arguments

In my application, ExecutorCompletionService is used to run couple of tasks which implement Callable. 在我的应用程序中,ExecutorCompletionService用于运行几个实现Callable的任务。 When the tasks are submitted to ExecutorCompletionServer, it gives compile error saying 当任务提交给ExecutorCompletionServer时,它会给出编译错误提示

The method submit(Callable<T>) in the type ExecutorCompletionService<T> is not applicable for the arguments (AbstractTask<capture#2-of ? extends Object>)  

This is how I submit tasks`. 这就是我提交任务的方式。

List<AbstractTask<? extends Object>> taskList =new ArrayList<>();
        addTasks(new TaskA()); //TaskA,TaskB,TaskC are child classses of AbstractTask. addTasks() method add the child classes to taskList.
        addTasks(new TaskB());
        addTasks(new TaskC());
        List<Future<? extends Object>> futureList= new ArrayList<Future <? extends Object> >();

        final ExecutorService pool = Executors.newFixedThreadPool( TASK_SIZE );

        final ExecutorCompletionService<T> completionService = new ExecutorCompletionService<T >(pool);

        for ( AbstractTask<? extends Object>  callable : taskList) {
                futureList.add( completionService.submit(callable) );
        }  

This is addTaskMethod. 这是addTaskMethod。

public void addTasks(AbstractTask<? extends Object> task){
            taskList.add(task);
        }

This is my AbstractClass. 这是我的AbstractClass。

public abstract class AbstractTask<T extends Object> implements
        java.util.concurrent.Callable<T> {

    @Override
    public T call() throws Exception {

        return begin();
    }
    public abstract <B extends Object> B begin() throws RuntimeException ;

}

Even the following method 'submit()' in java.util.concurrent.ExecutorCompletionService<V> accept arguement as 甚至java.util.concurrent.ExecutorCompletionService<V>的以下方法'submit()'也接受论点为

public Future<V> submit(Callable<V> task) {
    if (task == null) throw new NullPointerException();
    RunnableFuture<V> f = newTaskFor(task);
    executor.execute(new QueueingFuture(f));
    return f;
}   

Why I can't submit my AbstractTask<? extends Object> 为什么我不能提交AbstractTask<? extends Object> AbstractTask<? extends Object> ? AbstractTask<? extends Object> My AbstractTask can have any value which extends from Object. 我的AbstractTask可以具有任何从Object扩展的值。 (If I simple remove the <? extends Object> in for loop, then it fines.But again My tool:Eclipse warns Type safety: The expression of type AbstractTask needs unchecked conversion to conform to Callable<T> . How can I sortout this ? ) (如果我在for循环中简单地删除<? extends Object> ,它将很好。但是我的工具:Eclipse警告Type safety: The expression of type AbstractTask needs unchecked conversion to conform to Callable<T> 。如何解决这个问题? ?)

EDIT: This is TaskA class. 编辑:这是TaskA类。

public class TaskA  extends AbstractTask<Object> {

    @SuppressWarnings("unchecked")
    @Override
    public java.lang.String begin() throws RuntimeException {
        System.out.println("Task A runs");
        return "A";
    }

}

You're creating an ExecutorCompletionService<T> . 您正在创建ExecutorCompletionService<T> This is thus an ExecutorCompletionService that accepts instances of Callable<T> . 因此,这是一个ExecutorCompletionService,它接受Callable<T>实例。

But you're submitting instances of AbstractTask<? extends Object> 但是您要提交AbstractTask<? extends Object>实例AbstractTask<? extends Object> AbstractTask<? extends Object> . AbstractTask<? extends Object> A Callable<? extends Object> Callable<? extends Object> Callable<? extends Object> is not a Callable<T> . Callable<? extends Object>不是Callable<T>

A Callable<T> returns instances of T. A Callable<? extends Object> Callable<T>返回Callable<T>实例Callable<? extends Object> Callable<? extends Object> returns some unknown type, for which all we know is that the type extends Object. Callable<? extends Object>返回一些未知的类型,我们所知道的是该类型扩展了Object。 So that's basically like wanting to put unknown objects into a paper shredder . 因此,这基本上就像是要将未知对象放入碎纸机中。 The paper shredder is only able to shred paper, not any kind of object. 切纸机只能切纸,不能切碎任何物体。 The compiler prevents you from doing that, because it obviously won't work fine. 编译器阻止您这样做,因为它显然无法正常工作。

You need an ExecutorCompletionService<Object> . 您需要一个ExecutorCompletionService<Object>

In the following line we are creating an instance of ExecutorCompletionService<T> 在以下行中,我们将创建ExecutorCompletionService<T>的实例。

final ExecutorCompletionService<T> completionService = new ExecutorCompletionService<T >(pool);

It will only accept Callable<T> and submit will return Future<T> and when task is over each Future<T> will return result (using get ) of type T . 它仅接受Callable<T>submit将返回Future<T>并且当任务结束时,每个Future<T>将返回类型T结果(使用get )。

If we check the signature of submit method in ExecutorCompletionService : 如果我们在ExecutorCompletionService检查submit方法的签名:

public Future<V> submit(Callable<V> task)

Then it specifies the type variable V such that all the callables should be of same type V . 然后,它指定类型变量V ,以便所有可调用对象都应具有相同的类型V IMO, Executor Completion Service will not allow a task of type Callable<? extends Object> IMO,执行人完成服务将不允许Callable<? extends Object>类型的任务Callable<? extends Object> Callable<? extends Object> to be submitted because it is like saying that my task extends Object which means it can take almost every thing. Callable<? extends Object>提交,因为这就像说我的任务扩展了Object意味着这几乎可以处理所有事情。 So we cannot use unbound wildcard with ECS and had to restrict the possible result. 因此,我们不能在ECS中使用未绑定的通配符,而不得不限制可能的结果。

On a side note I believe Callable<? extends Object> 附带一提,我相信Callable<? extends Object> Callable<? extends Object> and Callable<?> are same. Callable<? extends Object>Callable<?>相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM