为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

弑天下 2022-05-25 16:01:01
调用多个方法,我想同时调用它们,应该等待彼此完成。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);}无法将两行放在线程内
查看完整描述

1 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

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


查看完整回答
反对 回复 2022-05-25
  • 1 回答
  • 0 关注
  • 79 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信