简体   繁体   English

如何在多线程java中执行行集

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

i have following class where spring create object for the class. 我有以下课程,春季为课程创建了对象。 i have created WebClient to make http request using CXF. 我创建了WebClient以使用CXF发出http请求。 i want to make 2 http call which i want to execute in 2 thread so that, i dont have to call the method twice . 我想进行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;
    }

}

In above code i want CustomerServiceProxy to be executed in one thread then vehicleServiceProxy in another and i want to aggregate the result once the two threads execution got completed. 在上面的代码中,我希望在一个线程中执行CustomerServiceProxy,然后在另一个线程中执行vehicleServiceProxy,一旦两个线程执行完成,我想汇总结果。

can somebody help me on this? 有人可以帮我吗?

You can use ExecutorService for this: 您可以为此使用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();

This will execute your logic concurrently and will aggregate the data until the Thread is done. 这将同时执行您的逻辑并将聚合数据,直到Thread完成。

You need a mix and an aggregator. 您需要一个混合和一个聚合器。

  • Either you can use threadPools and have callbacks 您可以使用threadPools并具有回调
  • or, you can use Apache Camel library as a reference 或者,您可以使用Apache Camel库作为参考

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 多线程无法执行 java 中的非同步代码 - Multiple thread failed to execute the nonsynchronized code in java 如何使用命令行在 maven 中执行 2 个或多个 java 文件? - How to execute 2 or multiple java file in maven using command line? Java:请举例说明“如何为多线程设置不同的时区?” - Java: Please provide an example on "How to set different timezone for multiple thread?" 如何在Java中同步一组多个线程相对于单个线程 - how to synchronize a set of multiple threads with respect to a single thread in Java 使用Java在命令行中执行多个命令 - Execute multiple commands in command line using Java Java线程:如何同时执行一条语句 - Java Thread: How to execute a statement simultaneously 如何在 Java 的同一线程上异步执行方法? - How to execute a method asynchronously on same thread in Java? 如何在java swing中同时执行两个线程? - how to execute two thread simultaneously in java swing? 如何使用数组在Java中的一行中设置多个选择条件? - How to use an array to set multiple selection critieria in one line in java? 如何通过单个命令行提示在java代码中执行多个命令? - How can we execute multiple commands in a java code by the help of single command line prompt?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM