我正在学习ExecutorService和Callables,但是得到intelij的警告说我正在为这一行做一个未选中的作业:
Future<OrderWorker<OfferType>> future = executorService.submit(new OrderWorker<OfferType>(anOfferSet));
我不知道如何正确分配。这是代码:
ExecutorService executorService = Executors.newCachedThreadPool();
List<Future<OrderWorker<OfferType>>> futures = new ArrayList<>();
for (OfferType anOfferSet : offerSet) {
// the next assignment is unchecked:
Future<OrderWorker<OfferType>> future = executorService.submit(new OrderWorker<OfferType>(anOfferSet));
futures.add(future);
}
订单工人:
public class OrderWorker<I extends OfferType> implements DataWorker {
private Logger log = Logger.getLogger(OrderWorker.class);
private final OfferType offer;
public OrderWorker(I data) {
this.offer = data;
}
public OfferType getOffer() {
return offer;
}
@Override
public Object call() throws Exception {
Thread.sleep(5000);
log.info(offer.getListingPrice().getAmount());
return this;
}
}
DataWorker界面:
interface DataWorker<I extends OfferType> extends Callable<I> {}
OfferType
是一个简单的POJO类。
编辑
我还尝试在实现时为DataWorker提供类型:
public class OrderWorker<I extends OfferType> implements DataWorker<I>
这给出了一个错误,说OrderWorker中的call()与Callable中的call()冲突 - &gt;不兼容的返回类型
我做错了什么,以及如何检查作业?
答案 0 :(得分:1)
您需要更改的是OrderWorker
的声明和call
的返回类型,如下所示:
public class OrderWorker<I extends OfferType> implements Callable<OrderWorker<I>> {
...
@Override
public OrderWorker<I> call() throws Exception {
...
}
}