如何将实现子接口的任务提交Callable<T>
给一个ExecutorService
?
我有一个子接口Callable<T>
定义为:
public interface CtiGwTask<T>
extends Callable {
...
}
它只是定义了一些静态常量,但没有添加任何方法。
然后我有以下方法哪里execService
是一个FixedThreadPool
实例。
@Override
public CtiGwTaskResult<Integer> postCtiTask(CtiGwTask<CtiGwTaskResult<Integer>> task) {
Future<CtiGwTaskResult<Integer>> result =
execService.submit(task);
try {
return result.get();
} catch (InterruptedException | ExecutionException ex) {
LOGGER.log(Level.FINEST,
"Could not complete CTIGwTask", ex);
return new CtiGwTaskResult<>(
CtiGwResultConstants.CTIGW_SERVER_SHUTTINGDOWN_ERROR,
Boolean.FALSE,
"Cannot complete task: CTIGateway server is shutting down.",
ex);
}
}
不幸的是,这给出了 2 个未经检查的转换和 1 个未经检查的方法调用警告。
...\CtiGwWorkerImpl.java:151: warning: [unchecked] unchecked conversion
execService.submit(task);
required: Callable<T>
found: CtiGwTask<CtiGwTaskResult<Integer>>
where T is a type-variable:
T extends Object declared in method <T>submit(Callable<T>)
...\CtiGwWorkerImpl.java:151: warning: [unchecked] unchecked method invocation: method submit in interface ExecutorService is applied to given types
execService.submit(task);
required: Callable<T>
found: CtiGwTask<CtiGwTaskResult<Integer>>
where T is a type-variable:
T extends Object declared in method <T>submit(Callable<T>)
...\CtiGwWorkerImpl.java:151: warning: [unchecked] unchecked conversion
execService.submit(task);
required: Future<CtiGwTaskResult<Integer>>
found: Future
如果我将submit
呼叫更改为
Future<CtiGwTaskResult<Integer>> result =
execService.submit( (Callable<CtiGwTaskResult<Integer>>) task);
然后一切似乎都正常,但现在我收到了未经检查的演员表警告。
...\src\com\dafquest\ctigw\cucm\CtiGwWorkerImpl.java:151: warning: [unchecked] unchecked cast
execService.submit((Callable<CtiGwTaskResult<Integer>>) task);
required: Callable<CtiGwTaskResult<Integer>>
found: CtiGwTask<CtiGwTaskResult<Integer>>
那么我错过了什么?不应该submit()
应用于 Callable 子类的实例吗?