如何将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子类的实例吗?
答案 0 :(得分:9)
您使用的是原始 Callable
类型。
变化:
public interface CtiGwTask<T> extends Callable
到此:
public interface CtiGwTask<T> extends Callable<T>