簡體   English   中英

如何在多線程java中執行行集

[英]how to execute the set of line in multiple thread java

我有以下課程,春季為課程創建了對象。 我創建了WebClient以使用CXF發出http請求。 我想進行2個我想在2個線程中執行的http調用,所以我不必兩次調用該方法。

@Service
public class SalesManServiceWebClient {

    @Value("${vehicle.api.url}")
    private String vehicleUrl;

    @Value("${customer.api.url}")
    private String customerUrl;

    private static final Logger LOG = LoggerFactory.getLogger(SalesManServiceWebClient.class);

    public List<String> getListOfDmsId(String market,STouchServiceEnum stouchService)
    {
        List<String> dmsIdList= new ArrayList<>();
        LOG.info("---Entering getListOfDmsId webClientService---");

        LOG.info("customerUrl:"+customerUrl);
        final String url = getServiceUrl(stouchService);

       final List<Object> providers = new ArrayList<Object>();
        providers.add(new JacksonJsonProvider());



        //i want this to be execute in thread
        CustomerServiceProxy proxy = JAXRSClientFactory.create(url, CustomerServiceProxy.class, providers);

        Client client = WebClient.client(proxy);
        client.type(MediaType.APPLICATION_JSON);
        //client.accept(MediaType.APPLICATION_JSON);
        client.header("ST_USER", "gasid3");
        client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE);
        LOG.info("== Invoking REST Call for service ==" + url);

        //till this 


        //i want this to be execute in thread
        VehicleServiceProxy proxy = JAXRSClientFactory.create(url, VehicleServiceProxy.class, providers);

        Client client = WebClient.client(proxy);
        client.type(MediaType.APPLICATION_JSON);
        //client.accept(MediaType.APPLICATION_JSON);
        client.header("ST_USER", "gasid3");
        client.header("Content-Type", MediaType.APPLICATION_JSON_TYPE);
        LOG.info("== Invoking REST Call for service ==" + url);

        //till this 

        /*Set<String> UniqueDmsId = new HashSet<>(dmsIdList);
        dmsIdList.clear();
        dmsIdList.addAll(UniqueDmsId);*/

         return dmsIdList;
    }




    private String getServiceUrl(STouchServiceEnum stouchService)
    {
        String url="";
        switch(stouchService)
        {
        case CUSTOMER:
            //url="http://BLRKEC327951D:8080/stouch-admin-services/api";
            url=customerUrl;
            //url=customerUrl.concat("/User/dmsMap");
            break;
        case VEHICLE:
            url=vehicleUrl.concat("/User/dmsMap");;
            break;
        default:
            break;
        }
        return url;
    }

}

在上面的代碼中,我希望在一個線程中執行CustomerServiceProxy,然后在另一個線程中執行vehicleServiceProxy,一旦兩個線程執行完成,我想匯總結果。

有人可以幫我嗎?

您可以為此使用ExecutorService

ExecutorService executor = Executors.newFixedThreadPool(2);
List<Future<String>> futures = new ArrayList<>();

Future<String> future1 = executor.submit(new Callable<String>() {
    @Override
    public String call() throws Exception {
        // Add first proxy logic here and return the results

        String jaxRsOutput = /* Gather output */;
        return jaxRsOutput;
    }
});

futures.add(future1);

Future<String> future2 = executor.submit(new Callable<String>() {
    @Override
    public String call() throws Exception {
        // Add second proxy logic here and return the results

        String jaxRsOutput = /* Gather output */;
        return jaxRsOutput;
    }
});

futures.add(future2);

for (Future<String> stringFuture : futures) {

    try {
        // .get blocks until thread is done
        String output = stringFuture.get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }
}

executor.shutdown();

這將同時執行您的邏輯並將聚合數據,直到Thread完成。

您需要一個混合和一個聚合器。

  • 您可以使用threadPools並具有回調
  • 或者,您可以使用Apache Camel庫作為參考

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM