简体   繁体   English

Rest 并行调用服务 - java 中的多线程

[英]Rest parellel calls to service -Multithreading in java

I have a rest call api where max count of result return by the api is 1000.start page=1我有一个 rest 调用 api ,其中 api 返回的最大结果数为 1000.start page=1

{ "status": "OK", "payload": { "EMPList":[], count:5665 } { “状态”:“OK”,“有效载荷”:{ “EMPList”:[],计数:5665 }

So to get other result I have to change the start page=2 and again hit the service.again will get 1000 results only.因此,要获得其他结果,我必须更改起始页 = 2 并再次点击服务。再次将仅获得 1000 个结果。

but after first call i want to make it as a parallel call and I want to collect the result and combine it and send it back to calling service in java.但是在第一次调用之后,我想将其作为并行调用,我想收集结果并将其组合并将其发送回 java 中的调用服务。 Please suggest i am new to java.i tried using callable but it's not working请建议我是 java 的新手。我尝试使用 callable 但它不起作用

It seems to me that ideally you should be able to configure your max count to one appropriate for your use case.在我看来,理想情况下,您应该能够将最大计数配置为适合您的用例的一个。 I'm assuming you aren't able to do that.我假设你无法做到这一点。 Here is a simple, lock-less, multi threading scheme that acts as a simple reduction operation for your two network calls:这是一个简单的、无锁的多线程方案,它充当两个网络调用的简单归约操作:

// online runnable: https://ideone.com/47KsoS
int resultSize = 5;
int[] result = new int[resultSize*2];

Thread pg1 = new Thread(){
    public void run(){
        System.out.println("Thread 1 Running...");
        // write numbers 1-5 to indexes 0-4
        for(int i = 0 ; i < resultSize; i ++) {
            result[i] = i + 1;
        }
        System.out.println("Thread 1 Exiting...");
    }
};

Thread pg2 = new Thread(){
    public void run(){
        System.out.println("Thread 2 Running");
        // write numbers 5-10 to indexes 5-9
        for(int i = 0 ; i < resultSize; i ++) {
            result[i + resultSize] = i + 1 + resultSize;
        }
        System.out.println("Thread 2 Exiting...");
    }
};

pg1.start();
pg2.start();

// ensure that pg1 execution finishes
pg1.join();
// ensure that pg2 execution finishes
pg2.join();

// print result of reduction operation
System.out.println(Arrays.toString(result));

There is a very important caveat with this implementation however.但是,此实现有一个非常重要的警告。 You will notice that both of the threads DO NOT overlap in their memory writes.您会注意到两个线程在其 memory 写入中不重叠。 This is very important as if you were to simply change our int[] result to ArrayList<Integer> this could lead to catastrophic failure in our reduction operation between the two threads called a Race Condition (I believe the standard ArrayList implementation in Java is not thread safe).这非常重要,就好像您只是将我们的int[] result更改为ArrayList<Integer>这可能会导致我们在两个线程之间的归约操作中发生灾难性故障,称为竞争条件(我相信 Java 中的标准 ArrayList 实现不是线程安全)。 Since we can guarantee how large our result will be I would highly suggest sticking to my usage of an array for this multi-threaded implementation as ArrayLists hide a lot of implementation logic from you that you likely won't understand until you take a basic data-structures course.因为我们可以保证我们的结果会有多大,所以我强烈建议坚持我对这个多线程实现的数组的使用,因为 ArrayLists 对你隐藏了很多实现逻辑,在你获取基本数据之前你可能不会理解-结构课程。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM