GCD

本文介绍了使用GCD进行线程管理的四种不同场景:异步并行、异步串行、同步并行及同步串行,并通过示例代码详细解释了每种场景下任务的执行顺序与特点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在处理复杂的业务逻辑的时候会经常使用到线程,常用到GCD
处理线程逻辑。
这里写图片描述

这里写图片描述

<—GCD使用组合–>

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // Insert code here to initialize your application
//    [self asyncConsurrent];
//    [self asyncSerial];
//    [self syncConcurrent];
//    [self syncSerial];
}

// 异步执行 + 并行执行
- (void)asyncConsurrent {
    // 创建一个并行队列
    dispatch_queue_t queue = dispatch_queue_create("async", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"----start----");
    dispatch_async(queue, ^{
        NSLog(@"-----任务1----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----任务2----%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"-----任务3----%@", [NSThread currentThread]);
    });
    NSLog(@"----end----%@", [NSThread currentThread]);
    /**
      异步执行意味着
         可以开启新的线程
         任务可以先绕过不执行,回过头来执行
      并行队列意味着
         任务之间不需要排队,具有同时被之执行的权利
      两者组合后的结果
         开启 三个(多个) 新的线程
         函数在执行时,先打印start和end,再回头执行这三个任务
         这三个任务是同时执行的,没有先后,所以打印结果为 1-->2-->3(这个结果是随机的)

     */
}
结果:
/*
2018-09-04 10:38:05.828888+0800 SocketDemo[40420:788589] ----start----
2018-09-04 10:38:05.830404+0800 SocketDemo[40420:788660] -----任务2----<NSThread: 0x608000275a00>{number = 3, name = (null)}
2018-09-04 10:38:05.830401+0800 SocketDemo[40420:788649] -----任务3----<NSThread: 0x600000464900>{number = 4, name = (null)}
2018-09-04 10:38:05.830402+0800 SocketDemo[40420:788589] ----end----<NSThread: 0x608000063dc0>{number = 1, name = main}
2018-09-04 10:38:05.830450+0800 SocketDemo[40420:788648] -----任务1----<NSThread: 0x600000464540>{number = 2, name = (null)}
*/

// 异步执行 + 串行队列
- (void)asyncSerial {
    // 创建一个串行队列
    dispatch_queue_t queue = dispatch_queue_create("async", DISPATCH_QUEUE_SERIAL);
    NSLog(@"----start----");
    // 使用异步函数创建三个任务
    dispatch_async(queue, ^{
        NSLog(@"---任务1---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        sleep(2);
        NSLog(@"---任务2---%@", [NSThread currentThread]);
    });
    dispatch_async(queue, ^{
        NSLog(@"---任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"-----end-----");

    /**
     异步执行意味着
        可以开启新的线程
        任务可以先绕过不执行回头再来执行
     串行队列意味着
        任务必须按添加进队列的顺序挨个执行
     两者组合结果
        开启 一个 新的子线程
        函数在执行时,先打印start和end再回头执行这三个任务
        这三个任务是顺序执行的,打印结果是1 --> 2 --> 3
     */
}
结果:
/*
2018-09-04 10:44:15.946637+0800 SocketDemo[40561:792462] ----start----
2018-09-04 10:44:15.947130+0800 SocketDemo[40561:792512] ---任务1---<NSThread: 0x600000274f40>{number = 2, name = (null)}
2018-09-04 10:44:15.947125+0800 SocketDemo[40561:792462] -----end-----<NSThread: 0x608000065440>{number = 1, name = main}
2018-09-04 10:44:17.952055+0800 SocketDemo[40561:792512] ---任务2---<NSThread: 0x600000274f40>{number = 2, name = (null)}
2018-09-04 10:44:17.952245+0800 SocketDemo[40561:792512] ---任务3---<NSThread: 0x600000274f40>{number = 2, name = (null)}

*/

// 同步执行 + 并行执行
- (void)syncConcurrent {
    dispatch_queue_t queue = dispatch_queue_create("sync", DISPATCH_QUEUE_CONCURRENT);
    NSLog(@"---start---");
    dispatch_sync(queue, ^{
        NSLog(@"----任务1----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"----任务2----%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"----任务3----%@", [NSThread currentThread]);
    });
    NSLog(@"-----end-----");

    /**
     同步执行意味着
        不能开启新的线程
        任务创建后必须执行完才能往下走
     并行队列意味着
         任务必须按照添加进队列的顺序挨个执行
     两者结合的结果
         所有任务都只能在主线程中执行
         函数在执行时,必须按照代码的书写顺序一行一行地执行完才能继续
     注意事项
      在这里即便是并行队列,任务可以同时执行,但是由于只存在一个主线程,所以没办法把任务发到不同的线程去同步处理,其结果就是只能在主线程里顺序挨个挨个执行了。
     */
};

结果:
/*
2018-09-04 10:46:54.086417+0800 SocketDemo[40633:794274] ---start---
2018-09-04 10:46:54.086652+0800 SocketDemo[40633:794274] ----任务1----<NSThread: 0x600000062c80>{number = 1, name = main}
2018-09-04 10:46:54.086714+0800 SocketDemo[40633:794274] ----任务2----<NSThread: 0x600000062c80>{number = 1, name = main}
2018-09-04 10:46:54.086762+0800 SocketDemo[40633:794274] ----任务3----<NSThread: 0x600000062c80>{number = 1, name = main}
2018-09-04 10:46:54.086862+0800 SocketDemo[40633:794274] -----end-----<NSThread: 0x600000062c80>{number = 1, name = main}
*/

// 同步串行
- (void)syncSerial{
    dispatch_queue_t queue = dispatch_queue_create("sunc", DISPATCH_QUEUE_SERIAL);
    NSLog(@"----start-----");
    dispatch_sync(queue, ^{
        NSLog(@"----任务1---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"----任务2---%@", [NSThread currentThread]);
    });
    dispatch_sync(queue, ^{
        NSLog(@"----任务3---%@", [NSThread currentThread]);
    });
    NSLog(@"----end-----");

    /**
     这里的执行原理和步骤图跟“同步执行+并发队列”是一样的,只要是同步执行就没法开启新的线程,所以多个任务之间也一样只能按顺序来执行,
     */
}

注 : 仅供自己使用记录。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值