iOS-多线程-(小码哥底层原理笔记)

iOS中常见的多线程方案

image.png

GCD的常用函数

同步方式执行任务

 dispatch_sync(dispatch_queue_t  _Nonnull queue, ^(void)block)

queue - 队列
block - 任务

  • 异步执行任务
dispatch_async(dispatch_queue_t  _Nonnull queue,^(void)block)

GCD的队列

并发队列
1.可以让多个任务同时执行(自动开启多个线程同时执行任务)
2.并发功能只有在异步(dispatch_async)函数下才有效

  • 串行队列
    1.让任务一个接一个执行(一个任务完成后才能执行下个任务)

注意:在ARC环境下,GCD创建的队列不需要手动release,但是使用CF开头的带有create需要手动release。

  • (dispatch_sync和dispatch_async)同步函数和异步函数只能决定开启新线程的能力((dispatch_get_main_queue())主队列的任务一定是在主线程执行的)
  • 串行和并发主要影响的是任务串行执行还是并发执行

image.png
总结来说:

  • 同步函数(dispatch_sync)和主队列都不会开启新线程,并且任务都是串行执行
  • 串行队列(手动创建的串行队列和主队列)都是串行执行任务的
  • 只有在异步函数中使用并发队列才会开启新线程同时并发执行任务

产生死锁

1.下面代码会不会产生死锁?

//代码1
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"任务1");
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_sync(queue, ^{
        NSLog(@"任务2");
    });
    NSLog(@"任务3");
}

答案:会产生死锁。viewDidLoad在主线程中执行,主队列中有任务viewDidLoad和任务2,根据队列先进先出的特点,任务2 必须要等到viewDidLoad执行完才能执行,同时因为执行任务2是在dispatch_sync中,根据dispatch_sync立马在当前线程执行任务,执行完毕后才能执行后续任务的特点,所以任务3在等任务2执行完,任务2在等viewDidLoad执行完,viewDidLoad要执行完必须是任务1,2,3都执行完。
image.png

  • dispatch_sync:立马在当前线程执行任务,执行完毕后才能执行后续任务
  • 队列的特点:FIFO(先进先出)

2.以上代码将dispatch_sync改为dispatch_async将不会产生死锁:

//代码2
- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"任务%@",[NSThread currentThread]);
    dispatch_queue_t queue = dispatch_get_main_queue();
    dispatch_async(queue, ^{
        NSLog(@"任务2%@",[NSThread currentThread]);
    });
    NSLog(@"任务3%@",[NSThread currentThread]);
}

打印结果

2020-11-28 13:10:02.165360+0800 多线程[52082:602232] 任务1<NSThread: 0x6000014f0100>{number = 1, name = main}
2020-11-28 13:10:02.165654+0800 多线程[52082:602232] 任务3<NSThread: 0x6000014f0100>{number = 1, name = main}
2020-11-28 13:10:02.180487+0800 多线程[52082:602232] 任务2<NSThread: 0x6000014f0100>{number = 1, name = main}

3.以下代码会不会产生死锁?

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    NSLog(@"任务1");
    dispatch_async(queue, ^{//block1
        NSLog(@"任务2");
        dispatch_sync(queue, ^{//block2
            NSLog(@"任务3");
        });
        NSLog(@"任务4");
    });
    NSLog(@"任务5");
}

答:会产生死锁。block1,block2都加入到了串行队列中,同时block2使用的是dispatch_sync,所以需要立马执行block2,但是block2必须要等到block1执行完后才能拿出来执行,所以产生了死锁。
4.将以上代码转换为将不会产生死锁

- (void)viewDidLoad {
    [super viewDidLoad];
    dispatch_queue_t queue = dispatch_queue_create("Queue", DISPATCH_QUEUE_SERIAL);
    dispatch_queue_t queue2 = dispatch_queue_create(@"Queue1", DISPATCH_QUEUE_SERIAL);
    NSLog(@"任务1");
    dispatch_async(queue, ^{
        NSLog(@"任务2");
        dispatch_sync(queue2, ^{
            NSLog(@"任务3");
        });
        NSLog(@"任务4");
    });
    NSLog(@"任务5");
}

打印结果

2020-11-28 13:23:13.103083+0800 多线程[52260:611080] 任务1
2020-11-28 13:23:13.103243+0800 多线程[52260:611080] 任务5
2020-11-28 13:23:13.103257+0800 多线程[52260:611214] 任务2
2020-11-28 13:23:13.103347+0800 多线程[52260:611214] 任务3
2020-11-28 13:23:13.103415+0800 多线程[52260:611214] 任务4
  • 总结:使用dispatch_sync函数往当前串行队列中加入任务会产生死锁

多线程和runloop

5.以下代码打印的结果是什么?

 1. (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        NSLog(@"任务1");
        [self performSelector:@selector(test) withObject:nil afterDelay:.0];//用到定时器
     
        NSLog(@"任务3");
    });
}
 2. (void)test {
    NSLog(@"任务2");
}

打印结果:

2020-11-30 12:28:47.436127+0800 多线程[8844:154622] 1
2020-11-30 12:28:47.436291+0800 多线程[8844:154622] 3

答:没有打印任务2的原因是因为[self performSelector:@selector(test) withObject:nil afterDelay:.0]这句代码是往runloop中添加了定时器,同时通过dispatch_async在子线程中执行任务,而子线程中是默认没有开启runloop的,所以不执行任务2。如果像下面的代码一样启动了runloop,则会打印任务2。

 3. (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        NSLog(@"1");
        //这句代码是往runloop中添加了定时器
        [self performSelector:@selector(test) withObject:nil afterDelay:.0];
        NSLog(@"3");
       // [[NSRunLoop currentRunLoop]addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
        //[NSDate distantPast]: 可以表示的最早的时间
        //[NSDate distantFuture]:可以表示的最远的未来时间
    });
}
 4. (void)test {
    NSLog(@"2");
}

6.以下代码会打印什么?

 5. (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSThread *thread = [[NSThread alloc]initWithBlock:^{
        NSLog(@"任务1");
    }];
    [thread start];
    [self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];
    
}
 6. (void)test {
    NSLog(@"任务2");
}

打印结果

2020-11-30 12:53:10.923568+0800 多线程[9289:175207] 任务1
2020-11-30 12:53:10.957516+0800 多线程[9289:174984] *** Terminating app due to uncaught exception 'NSDestinationInvalidException', reason: '*** -[ViewController performSelector:onThread:withObject:waitUntilDone:modes:]: target thread exited while waiting for the perform'
*** First throw call stack:

答:会打印任务1,然后崩溃。因为没有启动runloop,可改为以下代码:

 7. (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    NSThread *thread = [[NSThread alloc]initWithBlock:^{
        NSLog(@"任务1");
//        [[NSRunLoop currentRunLoop]addPort:[[NSPort alloc]init] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop]runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
    }];
    [thread start];
    [self performSelector:@selector(test) onThread:thread withObject:nil waitUntilDone:YES];
    
}
 8. (void)test {
    NSLog(@"任务2");
}

队列组的使用

7.如何用gcd实现以下功能
异步并发执行任务1,任务2
等任务1,任务2都执行完毕后,再回到主线程执行任务3

//创建队列组
    dispatch_group_t group = dispatch_group_create();
    //创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    //添加异步任务
    dispatch_group_async(group, queue, ^{
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务1-%@",[NSThread currentThread]);
        }
    });
    dispatch_group_async(group, queue, ^{
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务2-%@",[NSThread currentThread]);
        }
    });
    //等前面的任务执行完成后,会自动执行这个任务
    // dispatch_group_notify(group, queue, ^{//回到主线程
      //  dispatch_async(dispatch_get_main_queue(), ^{
       //     for (int i = 0 ;i < 5; i++) {
         //       NSLog(@"任务3-%@",[NSThread currentThread]);
           // }
        //});
    //});

    dispatch_group_notify(group, dispatch_get_main_queue(), ^{//回到主线程
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务3-%@",[NSThread currentThread]);
        }
    });

8.上述代码改为不回主线程,继续异步执行任务:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    //创建队列组
    dispatch_group_t group = dispatch_group_create();
    //创建并发队列
    dispatch_queue_t queue = dispatch_queue_create("myqueue", DISPATCH_QUEUE_CONCURRENT);
    //添加异步任务
    dispatch_group_async(group, queue, ^{
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务1-%@",[NSThread currentThread]);
        }
    });
    dispatch_group_async(group, queue, ^{
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务2-%@",[NSThread currentThread]);
        }
    });
    //等前面的任务执行完成后,会自动执行这个任务
    dispatch_group_notify(group, queue, ^{//回到主线程
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务3-%@",[NSThread currentThread]);
        }
    });
    dispatch_group_notify(group, queue, ^{//回到主线程
        for (int i = 0 ;i < 5; i++) {
            NSLog(@"任务4-%@",[NSThread currentThread]);
        }
    });
}

等任务1和任务2都执行完毕后,再交替执行任务3和任务4

参考链接:https://ke.qq.com/course/package/11609?quicklink=1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值