调用多个方法,我想同时调用它们,它们应该等待彼此完成。
else {
PricingFromS4Request pricingRequest = new PricingFromS4Request();
ProductFromS4Request productRequest = new ProductFromS4Request();
PricingFromS4ServiceImpl service = new PricingFromS4ServiceImpl();
//Create 1 thread for below line
pricingRequest = service.createS4PricingRequest(ABeanObject, SomeArrayList);
//Create 1 more thread for below line
productRequest = service.createS4ProductRequest(SomeList);
//Send pricingRequest and productRequest into another method
SomeMethod(pricingRequest,productRequest);
}
无法将两行放在线程内
答案 0 :(得分:0)
在Java 7中,这是一个选择:
PricingFromS4Request pricingRequest = new PricingFromS4Request();
ProductFromS4Request productRequest = new ProductFromS4Request();
PricingFromS4ServiceImpl service = new PricingFromS4ServiceImpl();
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<PricingFromS4Request> f1 = executor.submit(new Callable<PricingFromS4Request >() {
public PricingFromS4Request call() {
return service.createS4PricingRequest(ABeanObject, SomeArrayList);
}
});
Future<ProductFromS4Request> f2 = executor.submit(new Callable<ProductFromS4Request>() {
public ProductFromS4Request call() {
return service.createS4ProductRequest(SomeList);
}
});
SomeMethod(f1.get(), f2.get());
executor.shutdown();