猿问

同时调用多个返回返回对象的方法

调用多个方法,我想同时调用它们,应该等待彼此完成。


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);


}

无法将两行放在线程内


弑天下
浏览 81回答 1
1回答

肥皂起泡泡

在 Java 7 中,这将是一个选项:&nbsp; &nbsp; PricingFromS4Request pricingRequest = new PricingFromS4Request();&nbsp; &nbsp; ProductFromS4Request productRequest = new ProductFromS4Request();&nbsp; &nbsp; PricingFromS4ServiceImpl service = new PricingFromS4ServiceImpl();&nbsp; &nbsp; ExecutorService executor = Executors.newFixedThreadPool(2);&nbsp; &nbsp; Future<PricingFromS4Request> f1 = executor.submit(new Callable<PricingFromS4Request >() {&nbsp; &nbsp; &nbsp; &nbsp; public PricingFromS4Request&nbsp; call() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return service.createS4PricingRequest(ABeanObject, SomeArrayList);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; Future<ProductFromS4Request> f2 = executor.submit(new Callable<ProductFromS4Request>() {&nbsp; &nbsp; &nbsp; &nbsp; public ProductFromS4Request call() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return service.createS4ProductRequest(SomeList);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });&nbsp; &nbsp; SomeMethod(f1.get(), f2.get());&nbsp; &nbsp; executor.shutdown();
随时随地看视频慕课网APP

相关分类

Java
我要回答