• 周五. 12月 9th, 2022

5G编程聚合网

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

热门标签

GCD的6种组合试验

admin

11月 28, 2021

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

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t queue = dispatch_queue_create("queueName", NULL);
    dispatch_sync(queue, ^{
        NSLog(@">>>>>>>>>666");
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-09 14:26:53.868117+0800 OCTestLine[26738:2119182] >>>>>>>>>111
2021-08-09 14:26:53.868240+0800 OCTestLine[26738:2119182] >>>>>>>>>666
2021-08-09 14:26:53.868283+0800 OCTestLine[26738:2119182] >>>>>>>>>777
2021-08-09 14:26:53.868315+0800 OCTestLine[26738:2119182] >>>>>>>>>888
2021-08-09 14:26:53.868344+0800 OCTestLine[26738:2119182] >>>>>>>>>999
2021-08-09 14:26:53.868455+0800 OCTestLine[26738:2119182] >>>>>>>>>222
2021-08-09 14:26:53.868490+0800 OCTestLine[26738:2119182] >>>>>>>>>333
2021-08-09 14:26:53.868509+0800 OCTestLine[26738:2119182] >>>>>>>>>444
2021-08-09 14:26:53.868526+0800 OCTestLine[26738:2119182] >>>>>>>>>555
Program ended with exit code: 0

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

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@">>>>>>>>>666");
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-09 14:28:03.933092+0800 OCTestLine[26754:2120067] >>>>>>>>>111
(lldb) 

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

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

控制台的打印结果:

2021-08-09 14:29:20.474137+0800 OCTestLine[26791:2121857] >>>>>>>>>111
2021-08-09 14:29:20.474189+0800 OCTestLine[26791:2121857] >>>>>>>>>666
2021-08-09 14:29:20.474227+0800 OCTestLine[26791:2121857] >>>>>>>>>777
2021-08-09 14:29:20.474263+0800 OCTestLine[26791:2121857] >>>>>>>>>888
2021-08-09 14:29:20.474291+0800 OCTestLine[26791:2121857] >>>>>>>>>999
2021-08-09 14:29:20.474333+0800 OCTestLine[26791:2121857] >>>>>>>>>222
2021-08-09 14:29:20.474356+0800 OCTestLine[26791:2121857] >>>>>>>>>333
2021-08-09 14:29:20.474374+0800 OCTestLine[26791:2121857] >>>>>>>>>444
2021-08-09 14:29:20.474391+0800 OCTestLine[26791:2121857] >>>>>>>>>555
Program ended with exit code: 0

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

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t queue = dispatch_queue_create("queueName", NULL);
    dispatch_async(queue, ^{
        NSLog(@">>>>>>>>>666");
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-09 14:31:50.550148+0800 OCTestLine[26827:2123956] >>>>>>>>>111
2021-08-09 14:31:50.550465+0800 OCTestLine[26827:2123956] >>>>>>>>>222
2021-08-09 14:31:50.550483+0800 OCTestLine[26827:2123988] >>>>>>>>>666
2021-08-09 14:31:50.550511+0800 OCTestLine[26827:2123956] >>>>>>>>>333
2021-08-09 14:31:50.550521+0800 OCTestLine[26827:2123988] >>>>>>>>>777
2021-08-09 14:31:50.550539+0800 OCTestLine[26827:2123956] >>>>>>>>>444
2021-08-09 14:31:50.550548+0800 OCTestLine[26827:2123988] >>>>>>>>>888
2021-08-09 14:31:50.550568+0800 OCTestLine[26827:2123956] >>>>>>>>>555
2021-08-09 14:31:50.550572+0800 OCTestLine[26827:2123988] >>>>>>>>>999
Program ended with exit code: 0

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

- (void)test {
    NSLog(@">>>>>>>>>111");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@">>>>>>>>>666");
        NSLog(@">>>>>>>>>777");
        NSLog(@">>>>>>>>>888");
        NSLog(@">>>>>>>>>999");
    });
    NSLog(@">>>>>>>>>222");
    NSLog(@">>>>>>>>>333");
    NSLog(@">>>>>>>>>444");
    NSLog(@">>>>>>>>>555");
}

控制台的打印结果:

2021-08-10 14:24:01.516003+0800 OCTestLine[33821:2971842] >>>>>>>>>111
2021-08-10 14:24:01.516435+0800 OCTestLine[33821:2971842] >>>>>>>>>222
2021-08-10 14:24:01.516497+0800 OCTestLine[33821:2971842] >>>>>>>>>333
2021-08-10 14:24:01.516534+0800 OCTestLine[33821:2971842] >>>>>>>>>444
2021-08-10 14:24:01.516551+0800 OCTestLine[33821:2971842] >>>>>>>>>555
2021-08-10 14:24:01.516677+0800 OCTestLine[33821:2971842] >>>>>>>>>666
2021-08-10 14:24:01.516702+0800 OCTestLine[33821:2971842] >>>>>>>>>777
2021-08-10 14:24:01.516721+0800 OCTestLine[33821:2971842] >>>>>>>>>888
2021-08-10 14:24:01.516753+0800 OCTestLine[33821:2971842] >>>>>>>>>999
Program ended with exit code: 0

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

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

控制台的打印结果:

2021-08-09 14:33:36.189928+0800 OCTestLine[26868:2125874] >>>>>>>>>111
2021-08-09 14:33:36.190261+0800 OCTestLine[26868:2125898] >>>>>>>>>666
2021-08-09 14:33:36.190277+0800 OCTestLine[26868:2125874] >>>>>>>>>222
2021-08-09 14:33:36.190314+0800 OCTestLine[26868:2125898] >>>>>>>>>777
2021-08-09 14:33:36.190321+0800 OCTestLine[26868:2125874] >>>>>>>>>333
2021-08-09 14:33:36.190337+0800 OCTestLine[26868:2125898] >>>>>>>>>888
2021-08-09 14:33:36.190351+0800 OCTestLine[26868:2125874] >>>>>>>>>444
2021-08-09 14:33:36.190356+0800 OCTestLine[26868:2125898] >>>>>>>>>999
2021-08-09 14:33:36.190378+0800 OCTestLine[26868:2125874] >>>>>>>>>555
Program ended with exit code: 0

发表回复

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