iOS 开发 Objective-C

iOS 底层 day20 多线程 队列组 安全隐患 锁本质 自

2020-09-15  本文已影响0人  望穿秋水小作坊

一、多线程和 RunLoop 的碰撞

1. 请问下面的代码,点击页面后打印顺序是什么?
代码
2. 请问下面的代码,点击页面后打印顺序是什么?
代码
3. -performSelector:withObject 的本质
objc 源码
4. GNUstep 是什么?
5. -performSelector:withObject:afterDelay: 的本质
`-performSelector:withObject:afterDelay:` 的本质
6. 巩固练习,下面代码会发生什么?
题目

二、GCD 队列组

1. 代码实现:任务1 和任务2 异步并发执行,等任务1 和任务2 执行完毕,回到主线程执行 任务3
- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_group_t group = dispatch_group_create();
    
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    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, dispatch_get_main_queue(), ^{
        for (int i = 0 ; i < 5; i++) {
            NSLog(@"任务3 %@", [NSThread currentThread]);
        }
    });
}
2. 代码实现:任务1 和任务2 异步并发执行,等任务1 和任务2 执行完毕,并发执行 任务3 和任务4
- (void)viewDidLoad {
    [super viewDidLoad];
    
    dispatch_group_t group = dispatch_group_create();
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    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. 多线程安全隐患的解决方案
加锁

四、多线程同步与锁的本质

1. 为什么需要锁?
2. 对谁加锁?
3. 锁的本质?

五、自旋锁 NSSpinLock

1. 锁让线程阻塞有两种方式
2. 自旋锁 NSSpinLock 简介
3. 代码实现存取钱(未加锁)
- (void)moneyTest {
    self.moneyCount = 1000;
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        for (int i = 0 ; i < 10; i++) {
        [self drawMoney];
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0 ; i < 10; i++) {
        [self saveMoney];
        }
    });
    
}

- (void)drawMoney {
    int readCount = self.moneyCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount - 100;
    self.moneyCount = remainCount;
    NSLog(@"存钱---%@---剩余:%d",[NSThread currentThread], remainCount);
}

- (void)saveMoney {
    int readCount = self.moneyCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount + 200;
    self.moneyCount = remainCount;
    NSLog(@"取钱---%@---剩余:%d",[NSThread currentThread], remainCount);
}
4. 代码实现卖票(未加锁)

- (void)ticketTest {
    self.ticketsCount = 20;
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [self saleTicket];
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [self saleTicket];
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [self saleTicket];
        }
    });
}

- (void)saleTicket {
    int readCount = self.ticketsCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount - 1;
    self.ticketsCount = remainCount;
    NSLog(@"卖票---%@---剩余:%d",[NSThread currentThread], remainCount);
}
5. 代码实现 卖票 和 存取钱(加自旋锁)
#import "ViewController.h"
#import <libkern/OSAtomic.h>

OSSpinLock moneyLock = OS_SPINLOCK_INIT;
OSSpinLock ticketsLock = OS_SPINLOCK_INIT;

@interface ViewController ()
@property(nonatomic, assign) int moneyCount;
@property(nonatomic, assign) int ticketsCount;
@end

@implementation ViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    [self moneyTest];
//    [self ticketTest];
}
- (void)moneyTest {
    self.moneyCount = 1000;
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    dispatch_async(queue, ^{
        for (int i = 0 ; i < 10; i++) {
        [self drawMoney];
        }
    });
    
    dispatch_async(queue, ^{
        for (int i = 0 ; i < 10; i++) {
        [self saveMoney];
        }
    });
    
}

- (void)drawMoney {
    OSSpinLockLock(&moneyLock);
    int readCount = self.moneyCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount - 100;
    self.moneyCount = remainCount;
    NSLog(@"存钱---%@---剩余:%d",[NSThread currentThread], remainCount);
    OSSpinLockUnlock(&moneyLock);
}

- (void)saveMoney {
    OSSpinLockLock(&moneyLock);
    int readCount = self.moneyCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount + 200;
    self.moneyCount = remainCount;
    NSLog(@"取钱---%@---剩余:%d",[NSThread currentThread], remainCount);
    OSSpinLockUnlock(&moneyLock);
}

- (void)ticketTest {
    self.ticketsCount = 20;
    dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
    
    dispatch_async(queue, ^{
        for (int i = 0; i < 10; i++) {
            [self saleTicket];
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [self saleTicket];
        }
    });
    dispatch_async(queue, ^{
        for (int i = 0; i < 5; i++) {
            [self saleTicket];
        }
    });
}

- (void)saleTicket {
    OSSpinLockLock(&ticketsLock);
    int readCount = self.ticketsCount;
    sleep(0.2); //模拟网络拥堵
    int remainCount = readCount - 1;
    self.ticketsCount = remainCount;
    NSLog(@"卖票---%@---剩余:%d",[NSThread currentThread], remainCount);
    OSSpinLockUnlock(&ticketsLock);
}
@end

上一篇 下一篇

猜你喜欢

热点阅读