search

Home  >  Q&A  >  body text

java - 发送10个网络请求,然后再接收到所有回应之后执行后续操作,如何实现?

我想到的

1

2

3

4

<code>for iOS

1.用dispatch_group实现

2.用RunLoop实现  

</code>

还有没有其他的比较好的实现方式,求关于并发编程的文章.

天蓬老师天蓬老师2949 days ago1789

reply all(21)I'll reply

  • 怪我咯

    怪我咯2017-04-17 16:17:12

    Someone said NSOperation, but I won’t say it.
    But I prefer GCD. This problem can be solved using barrier:

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    <code>    dispatch_queue_t queue = dispatch_queue_create("JOHNSHAW", DISPATCH_QUEUE_CONCURRENT);

         

        dispatch_async(queue, ^{

            NSLog(@"task 1");

            sleep(1);

        });

        dispatch_async(queue, ^{

            NSLog(@"task 2");

            sleep(1);

        });

         

        dispatch_barrier_async(queue, ^{

            NSLog(@"after task 1 and task 2");

            sleep(1);

        });

         

        dispatch_async(queue, ^{

            NSLog(@"do someting else");

        });

    </code>

    Actually, I prefer RAC, but RAC is a heavy-duty framework after all, and not many companies use it

    1

    2

    3

    4

    5

    6

    <code>    RACSignal *task1 = ...;

        RACSignal *task2 = ...;

         

        [[RACSignal combineLatest:@[task1,task2]] subscribeNext:^(id x) {

            NSLog(@"after task1 and task2 ");

        }];</code>

    reply
    0
  • 阿神

    阿神2017-04-17 16:17:12

    Use RxAndroid. A .zip method is all it takes. .

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:17:12

    A better way to use NSOperation is because of the addDependecy method~

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:17:12

    For javascript:

    1. https://github.com/caolan/async
      It’s very convenient to handle asynchronous parallelism and serialization

    2. Use jQuery’s Promise, or other libraries’ Promise implementation :)

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 16:17:12

    Learn from the map reduce method, which is similar to the idea of ​​merge sort

    reply
    0
  • 迷茫

    迷茫2017-04-17 16:17:12

    Just use dispatch_group_wait and dispatch_group_notify normally.

    You can also use dispatch_barrier_sync / dispatch_barrier_async to use the operation after receiving all responses as a barrier block. Then this block will wait for all network requests to complete before executing.

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:17:12

    I don’t know whether you are asking about Android or iOS. If it is Android, use CountDownLatch to control threads

    reply
    0
  • PHPz

    PHPz2017-04-17 16:17:12

    In Java, you can use the future api of the concurrent package to implement separate requests from multiple threads, and then process the results after all responses.

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 16:17:12

    for iOS
    Create a serial queue for processing. Does not affect external concurrent requests.

    reply
    0
  • 黄舟

    黄舟2017-04-17 16:17:12

    How to implement it in JS?

    reply
    0
  • Cancelreply