美文网首页
6个并发队列两两执行

6个并发队列两两执行

作者: 等消息的人 | 来源:发表于2019-03-31 23:23 被阅读0次

    有6个并发队列A1、A2、B1、B2、C1、C2,实现A1、A2执行完后执行B1、B2,B1、B2执行完后执行C1、C2
    使用GCD实现

    dispatch_queue_t queue = dispatch_queue_create("com.queue", DISPATCH_QUEUE_CONCURRENT);
        dispatch_async(queue, ^{
            NSLog(@"线程A1开始");
            sleep(1);
            NSLog(@"线程A1结束");
        });
        dispatch_async(queue, ^{
            NSLog(@"线程A2开始");
            sleep(1);
            NSLog(@"线程A2结束");
        });
        
        dispatch_barrier_async(queue, ^{
            dispatch_async(queue, ^{
                NSLog(@"线程B1开始");
                sleep(1);
                NSLog(@"线程B1结束");
            });
            dispatch_async(queue, ^{
                NSLog(@"线程B2开始");
                sleep(1);
                NSLog(@"线程B2结束");
            });
            dispatch_barrier_async(queue, ^{
                dispatch_async(queue, ^{
                    NSLog(@"线程C1开始");
                    sleep(1);
                    NSLog(@"线程C1结束");
                });
                dispatch_async(queue, ^{
                    NSLog(@"线程C2开始");
                    sleep(1);
                    NSLog(@"线程C2结束");
                });
            });
        });
    
    2019-03-31 23:18:47.958148+0800 0108数组字典[19814:705465] 线程A1开始
    2019-03-31 23:18:47.958154+0800 0108数组字典[19814:705464] 线程A2开始
    2019-03-31 23:18:48.962063+0800 0108数组字典[19814:705464] 线程A2结束
    2019-03-31 23:18:48.962063+0800 0108数组字典[19814:705465] 线程A1结束
    2019-03-31 23:18:48.962383+0800 0108数组字典[19814:705464] 线程B1开始
    2019-03-31 23:18:48.962383+0800 0108数组字典[19814:705465] 线程B2开始
    2019-03-31 23:18:49.964813+0800 0108数组字典[19814:705464] 线程B1结束
    2019-03-31 23:18:49.964814+0800 0108数组字典[19814:705465] 线程B2结束
    2019-03-31 23:18:49.965271+0800 0108数组字典[19814:705464] 线程C1开始
    2019-03-31 23:18:49.965279+0800 0108数组字典[19814:705465] 线程C2开始
    2019-03-31 23:18:50.967723+0800 0108数组字典[19814:705464] 线程C1结束
    2019-03-31 23:18:50.967723+0800 0108数组字典[19814:705465] 线程C2结束
    

    使用NSOperation实现相反顺序

    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
        queue.maxConcurrentOperationCount = 2;
        
        NSBlockOperation *operationA1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程A1开始");
            sleep(1);
            NSLog(@"线程A1结束");
        }];
        NSBlockOperation *operationA2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程A2开始");
            sleep(1);
            NSLog(@"线程A2结束");
        }];
        NSBlockOperation *operationB1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程B1开始");
            sleep(1);
            NSLog(@"线程B1结束");
        }];
        NSBlockOperation *operationB2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程B2开始");
            sleep(1);
            NSLog(@"线程B2结束");
        }];
        NSBlockOperation *operationC1 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程C1开始");
            sleep(1);
            NSLog(@"线程C1结束");
        }];
        NSBlockOperation *operationC2 = [NSBlockOperation blockOperationWithBlock:^{
            NSLog(@"线程C2开始");
            sleep(1);
            NSLog(@"线程C2结束");
        }];
        
        [operationA1 addDependency:operationB1];
        [operationA1 addDependency:operationB2];
        [operationA2 addDependency:operationB1];
        [operationA2 addDependency:operationB2];
        
        [operationB1 addDependency:operationC1];
        [operationB1 addDependency:operationC2];
        [operationB2 addDependency:operationC1];
        [operationB2 addDependency:operationC2];
        
        [queue addOperation:operationA1];
        [queue addOperation:operationA2];
        [queue addOperation:operationB1];
        [queue addOperation:operationB2];
        [queue addOperation:operationC1];
        [queue addOperation:operationC2];
    
    2019-03-31 23:24:51.753819+0800 0108数组字典[19872:715639] 线程C2开始
    2019-03-31 23:24:51.753822+0800 0108数组字典[19872:715642] 线程C1开始
    2019-03-31 23:24:52.756609+0800 0108数组字典[19872:715642] 线程C1结束
    2019-03-31 23:24:52.756614+0800 0108数组字典[19872:715639] 线程C2结束
    2019-03-31 23:24:52.756954+0800 0108数组字典[19872:715642] 线程B2开始
    2019-03-31 23:24:52.756956+0800 0108数组字典[19872:715641] 线程B1开始
    2019-03-31 23:24:53.761289+0800 0108数组字典[19872:715642] 线程B2结束
    2019-03-31 23:24:53.761310+0800 0108数组字典[19872:715641] 线程B1结束
    2019-03-31 23:24:53.761751+0800 0108数组字典[19872:715641] 线程A2开始
    2019-03-31 23:24:53.761755+0800 0108数组字典[19872:715642] 线程A1开始
    2019-03-31 23:24:54.764618+0800 0108数组字典[19872:715641] 线程A2结束
    2019-03-31 23:24:54.764618+0800 0108数组字典[19872:715642] 线程A1结束
    

    相关文章

      网友评论

          本文标题:6个并发队列两两执行

          本文链接:https://www.haomeiwen.com/subject/nhvpbqtx.html