• 周日. 11月 27th, 2022

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

GCD的另外6种组合试验

admin

11月 28, 2021

组合一:同步函数 + 自创串行队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_queue_create("queueName", NULL);
        dispatch_sync(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:32:37.906201+0800 OCTestLine[33934:2977686] >>>>>>>>>111
2021-08-10 14:32:37.906546+0800 OCTestLine[33934:2977686] >>>>>>>>>222
2021-08-10 14:32:37.906530+0800 OCTestLine[33934:2978055] >>>>>>>>>666
2021-08-10 14:32:37.906582+0800 OCTestLine[33934:2977686] >>>>>>>>>333
2021-08-10 14:32:37.906614+0800 OCTestLine[33934:2977686] >>>>>>>>>444
2021-08-10 14:32:37.906617+0800 OCTestLine[33934:2978055] >>>>>>>>>aaa
2021-08-10 14:32:37.906644+0800 OCTestLine[33934:2977686] >>>>>>>>>555
2021-08-10 14:32:37.906650+0800 OCTestLine[33934:2978055] >>>>>>>>>bbb
2021-08-10 14:32:37.906679+0800 OCTestLine[33934:2978055] >>>>>>>>>ccc
2021-08-10 14:32:37.906707+0800 OCTestLine[33934:2978055] >>>>>>>>>ddd
2021-08-10 14:32:37.906751+0800 OCTestLine[33934:2978055] >>>>>>>>>777
2021-08-10 14:32:37.906779+0800 OCTestLine[33934:2978055] >>>>>>>>>888
2021-08-10 14:32:37.906830+0800 OCTestLine[33934:2978055] >>>>>>>>>999

组合二:同步函数 + 主队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_get_main_queue();
        dispatch_sync(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:37:04.496324+0800 OCTestLine[34029:2981683] >>>>>>>>>111
2021-08-10 14:37:04.496658+0800 OCTestLine[34029:2981683] >>>>>>>>>222
2021-08-10 14:37:04.496627+0800 OCTestLine[34029:2982079] >>>>>>>>>666
2021-08-10 14:37:04.496694+0800 OCTestLine[34029:2981683] >>>>>>>>>333
2021-08-10 14:37:04.496722+0800 OCTestLine[34029:2981683] >>>>>>>>>444
2021-08-10 14:37:04.496742+0800 OCTestLine[34029:2981683] >>>>>>>>>555
2021-08-10 14:37:04.496845+0800 OCTestLine[34029:2981683] >>>>>>>>>aaa
2021-08-10 14:37:04.496870+0800 OCTestLine[34029:2981683] >>>>>>>>>bbb
2021-08-10 14:37:04.496888+0800 OCTestLine[34029:2981683] >>>>>>>>>ccc
2021-08-10 14:37:04.496905+0800 OCTestLine[34029:2981683] >>>>>>>>>ddd
2021-08-10 14:37:04.496941+0800 OCTestLine[34029:2982079] >>>>>>>>>777
Program ended with exit code: 0

组合三:同步函数 + 全局并发队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_sync(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:37:38.862942+0800 OCTestLine[34045:2982414] >>>>>>>>>111
2021-08-10 14:37:38.863252+0800 OCTestLine[34045:2982414] >>>>>>>>>222
2021-08-10 14:37:38.863234+0800 OCTestLine[34045:2982804] >>>>>>>>>666
2021-08-10 14:37:38.863283+0800 OCTestLine[34045:2982414] >>>>>>>>>333
2021-08-10 14:37:38.863304+0800 OCTestLine[34045:2982414] >>>>>>>>>444
2021-08-10 14:37:38.863322+0800 OCTestLine[34045:2982414] >>>>>>>>>555
2021-08-10 14:37:38.863324+0800 OCTestLine[34045:2982804] >>>>>>>>>aaa
2021-08-10 14:37:38.863357+0800 OCTestLine[34045:2982804] >>>>>>>>>bbb
2021-08-10 14:37:38.863384+0800 OCTestLine[34045:2982804] >>>>>>>>>ccc
2021-08-10 14:37:38.863408+0800 OCTestLine[34045:2982804] >>>>>>>>>ddd
2021-08-10 14:37:38.863453+0800 OCTestLine[34045:2982804] >>>>>>>>>777
2021-08-10 14:37:38.863536+0800 OCTestLine[34045:2982804] >>>>>>>>>888
2021-08-10 14:37:38.863596+0800 OCTestLine[34045:2982804] >>>>>>>>>999

组合四:异步函数 + 自创串行队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_queue_create("queueName", NULL);
        dispatch_async(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:38:18.512990+0800 OCTestLine[34061:2983212] >>>>>>>>>111
2021-08-10 14:38:18.513322+0800 OCTestLine[34061:2983212] >>>>>>>>>222
2021-08-10 14:38:18.513306+0800 OCTestLine[34061:2983598] >>>>>>>>>666
2021-08-10 14:38:18.513367+0800 OCTestLine[34061:2983212] >>>>>>>>>333
2021-08-10 14:38:18.513399+0800 OCTestLine[34061:2983212] >>>>>>>>>444
2021-08-10 14:38:18.513421+0800 OCTestLine[34061:2983598] >>>>>>>>>777
2021-08-10 14:38:18.513427+0800 OCTestLine[34061:2983212] >>>>>>>>>555
2021-08-10 14:38:18.513460+0800 OCTestLine[34061:2983598] >>>>>>>>>888
2021-08-10 14:38:18.513477+0800 OCTestLine[34061:2983599] >>>>>>>>>aaa
2021-08-10 14:38:18.513491+0800 OCTestLine[34061:2983598] >>>>>>>>>999
2021-08-10 14:38:18.513518+0800 OCTestLine[34061:2983599] >>>>>>>>>bbb
2021-08-10 14:38:18.513564+0800 OCTestLine[34061:2983599] >>>>>>>>>ccc
2021-08-10 14:38:18.513625+0800 OCTestLine[34061:2983599] >>>>>>>>>ddd

组合五:异步函数 + 主队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_get_main_queue();
        dispatch_async(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:39:24.050567+0800 OCTestLine[34078:2984134] >>>>>>>>>111
2021-08-10 14:39:24.050868+0800 OCTestLine[34078:2984134] >>>>>>>>>222
2021-08-10 14:39:24.050866+0800 OCTestLine[34078:2984512] >>>>>>>>>666
2021-08-10 14:39:24.050904+0800 OCTestLine[34078:2984134] >>>>>>>>>333
2021-08-10 14:39:24.050936+0800 OCTestLine[34078:2984134] >>>>>>>>>444
2021-08-10 14:39:24.050943+0800 OCTestLine[34078:2984512] >>>>>>>>>777
2021-08-10 14:39:24.050965+0800 OCTestLine[34078:2984134] >>>>>>>>>555
2021-08-10 14:39:24.050979+0800 OCTestLine[34078:2984512] >>>>>>>>>888
2021-08-10 14:39:24.051007+0800 OCTestLine[34078:2984512] >>>>>>>>>999
2021-08-10 14:39:24.051107+0800 OCTestLine[34078:2984134] >>>>>>>>>aaa
2021-08-10 14:39:24.051135+0800 OCTestLine[34078:2984134] >>>>>>>>>bbb
2021-08-10 14:39:24.051154+0800 OCTestLine[34078:2984134] >>>>>>>>>ccc
2021-08-10 14:39:24.051179+0800 OCTestLine[34078:2984134] >>>>>>>>>ddd
Program ended with exit code: 0

组合六:异步函数 + 全局并发队列

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t global_queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(global_queue, ^{
        NSLog(@">>>>>>>>>666");
        dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
        dispatch_async(queue, ^{
            NSLog(@">>>>>>>>>aaa");
            NSLog(@">>>>>>>>>bbb");
            NSLog(@">>>>>>>>>ccc");
            NSLog(@">>>>>>>>>ddd");
        });
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:40:04.387136+0800 OCTestLine[34091:2984975] >>>>>>>>>111
2021-08-10 14:40:04.387443+0800 OCTestLine[34091:2984975] >>>>>>>>>222
2021-08-10 14:40:04.387428+0800 OCTestLine[34091:2985374] >>>>>>>>>666
2021-08-10 14:40:04.387481+0800 OCTestLine[34091:2984975] >>>>>>>>>333
2021-08-10 14:40:04.387512+0800 OCTestLine[34091:2984975] >>>>>>>>>444
2021-08-10 14:40:04.387526+0800 OCTestLine[34091:2985374] >>>>>>>>>777
2021-08-10 14:40:04.387538+0800 OCTestLine[34091:2984975] >>>>>>>>>555
2021-08-10 14:40:04.387557+0800 OCTestLine[34091:2985375] >>>>>>>>>aaa
2021-08-10 14:40:04.387563+0800 OCTestLine[34091:2985374] >>>>>>>>>888
2021-08-10 14:40:04.387597+0800 OCTestLine[34091:2985374] >>>>>>>>>999
2021-08-10 14:40:04.387610+0800 OCTestLine[34091:2985375] >>>>>>>>>bbb
2021-08-10 14:40:04.387691+0800 OCTestLine[34091:2985375] >>>>>>>>>ccc
2021-08-10 14:40:04.387738+0800 OCTestLine[34091:2985375] >>>>>>>>>ddd

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注