美文网首页
GCD相关题目

GCD相关题目

作者: 脚踏实地的小C | 来源:发表于2021-06-07 08:59 被阅读0次

1、以下代码结果会如何?

    NSLog(@"1===========%@",[NSThread currentThread]);
    dispatch_sync(dispatch_get_main_queue(), ^{
        NSLog(@"2===========%@",[NSThread currentThread]);
    });
    NSLog(@"3===========%@",[NSThread currentThread]);

结果如下:

1===========<NSThread: 0x6000037b0600>{number = 1, name = main}

会造成死锁,主线程中【同步执行+主队列】,造成的互相等待。

2、写一个线程安全的购票代码

- (void)initTicketStatusSave {
    
    NSLog(@"start_%@",[NSThread currentThread]);
    
    semaphoreLock = dispatch_semaphore_create(1);
    self.ticketSurplusCount = 6;
    //北京售票口
    dispatch_queue_t queueBJ = dispatch_queue_create("beijin", DISPATCH_QUEUE_SERIAL);
    //上海售票口    
    dispatch_queue_t queueSH = dispatch_queue_create("shanghai",DISPATCH_QUEUE_SERIAL);
    
    __weak typeof(self) weakSelf = self;
    dispatch_async(queueBJ, ^{
        [weakSelf saleTicketSafe];
    });
    
    dispatch_async(queueSH, ^{
        [weakSelf saleTicketSafe];
    });
    
}

- (void)saleTicketSafe {
    
    while (1) {
        dispatch_semaphore_wait(semaphoreLock, DISPATCH_TIME_FOREVER);
        
        if (self.ticketSurplusCount > 0) {
            self.ticketSurplusCount--;
            NSLog(@"%@",[NSString stringWithFormat:@"剩余票数:%ld 窗口:%@",self.ticketSurplusCount,[NSThread currentThread]]);
            [NSThread sleepForTimeInterval:0.2];
        } else {
            NSLog(@"所有火车票均已售完");
            dispatch_semaphore_signal(semaphoreLock);
            break;;
        }
        dispatch_semaphore_signal(semaphoreLock);
    }
}
start_<NSThread: 0x6000021641c0>{number = 1, name = main}
剩余票数:5 窗口:<NSThread: 0x6000021246c0>{number = 5, name = (null)}
剩余票数:4 窗口:<NSThread: 0x60000216dbc0>{number = 6, name = (null)}
剩余票数:3 窗口:<NSThread: 0x6000021246c0>{number = 5, name = (null)}
剩余票数:2 窗口:<NSThread: 0x60000216dbc0>{number = 6, name = (null)}
剩余票数:1 窗口:<NSThread: 0x6000021246c0>{number = 5, name = (null)}
剩余票数:0 窗口:<NSThread: 0x60000216dbc0>{number = 6, name = (null)}
所有火车票均已售完
所有火车票均已售完

3、请说出下列的打印顺序

- (void)GCDForTest {
    
    dispatch_queue_t testQueue = dispatch_queue_create("testQueue", DISPATCH_QUEUE_SERIAL);
    
    NSLog(@"1 ---- %@",[NSThread currentThread]);
    
    dispatch_async(testQueue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"2 ---- %@",[NSThread currentThread]);
    });
    
    NSLog(@"3 ---- %@",[NSThread currentThread]);
    
    dispatch_sync(testQueue, ^{
        [NSThread sleepForTimeInterval:1];
        NSLog(@"4 ---- %@",[NSThread currentThread]);
    });
    
    NSLog(@"5 ---- %@",[NSThread currentThread]);
}

打印顺序为:1、3、2、4、5,你说对了吗?

1 ---- <NSThread: 0x6000035b4500>{number = 1, name = main}
3 ---- <NSThread: 0x6000035b4500>{number = 1, name = main}
2 ---- <NSThread: 0x6000035b0d40>{number = 5, name = (null)}
4 ---- <NSThread: 0x6000035b4500>{number = 1, name = main}
5 ---- <NSThread: 0x6000035b4500>{number = 1, name = main}

     在任务1执行完后,将任务2添加至串行队列中,由于是异步的,所以不会堵塞线程。继续往下执行任务3,然后将任务4添加至串行队列,任务2任务4此时在同一个串行队列,根据队列的先进先出原则,同步执行的任务4必须得等任务2执行完后才能执行,不然会堵塞线程。

4、下面的代码会如何打印

- (void)demoForSelector {
    
    NSLog(@"1 ---- %@",[NSThread currentThread]);
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        NSLog(@"2 ---- %@",[NSThread currentThread]);
        [self performSelector:@selector(testForSelector:) withObject:@"performSelector" afterDelay:1];
    });
    NSLog(@"3 ---- %@",[NSThread currentThread]);
}
- (void)testForSelector:(NSString *)msg {
    NSLog(@"%@ ---- %@",msg,[NSThread currentThread]);
}
1 ---- <NSThread: 0x600000f10240>{number = 1, name = main}
3 ---- <NSThread: 0x600000f10240>{number = 1, name = main}
2 ---- <NSThread: 0x600000f1a180>{number = 5, name = (null)}

可以看到,testForSelector:方法没走,为什么?
     是因为在GCD异步全局并发队列的子线程里,默认是没有开启对应的RunLoop的,所以这个方法就会失效。

有3中方式可以让这个方法起作用:

  • dispatch_get_global_queue换成dispatch_get_main_queue
  • dispatch_async换成dispatch_async
  • testForSelector:方法添加到RunLoop里
NSRunLoop *loop = [NSRunLoop currentRunLoop];
[self performSelector:@selector(testForSelector:) withObject:@"performSelector" afterDelay:0.1];
[loop run];

5、怎么用GCD实现多读单写?

多读单写:可以多个读者同时读取数据,但是在读的时候不能去写入数据。写的时候,只能单个写,不能有其他写者同时去写。

- (void)多读单写 {
    
    dispatch_queue_t queue = dispatch_queue_create("duodudanxie", DISPATCH_QUEUE_CONCURRENT);
    
    //读
    for (int i = 0; i < 5; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d ---- %@",i,[NSThread currentThread]);
        });
    }
    //写
    dispatch_barrier_sync(queue, ^{
        NSLog(@"barrier ---- %@",[NSThread currentThread]);
    });
    //读
    for (int i = 10; i < 5; i++) {
        dispatch_async(queue, ^{
            NSLog(@"%d ---- %@",i,[NSThread currentThread]);
        });
    }
}
0 ---- <NSThread: 0x6000008b1140>{number = 5, name = (null)}
1 ---- <NSThread: 0x6000008fc8c0>{number = 6, name = (null)}
3 ---- <NSThread: 0x6000008e8580>{number = 7, name = (null)}
2 ---- <NSThread: 0x6000008e1dc0>{number = 3, name = (null)}
4 ---- <NSThread: 0x6000008e4d00>{number = 8, name = (null)}
barrier ---- <NSThread: 0x6000008b4a00>{number = 1, name = main}
10 ---- <NSThread: 0x6000008e4d00>{number = 8, name = (null)}
13 ---- <NSThread: 0x6000008e1d00>{number = 4, name = (null)}
11 ---- <NSThread: 0x6000008e1dc0>{number = 3, name = (null)}
12 ---- <NSThread: 0x6000008e8580>{number = 7, name = (null)}
14 ---- <NSThread: 0x6000008fc8c0>{number = 6, name = (null)}

6、在多个网络请求完成后才去刷新UI界面

在n个耗时并发任务都完成后,再去执行接下来的任务。

  • dispatch_group_async
- (void)demoForGroup {
    
    dispatch_queue_t concurrentQueue = dispatch_queue_create("groupqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_group_t group = dispatch_group_create();
    for (NSInteger i = 0; i < 5; i++) {
        dispatch_group_async(group, concurrentQueue, ^{
            [NSThread sleepForTimeInterval:1];
            NSLog(@"网络请求:%ld ---- %@",i,[NSThread currentThread]);
        });
    }
    
    dispatch_group_notify(group, concurrentQueue, ^{
        NSLog(@"刷新UI ---- %@",[NSThread currentThread]);
    });
}
网络请求:2 ---- <NSThread: 0x600001b15240>{number = 6, name = (null)}
网络请求:0 ---- <NSThread: 0x600001b19c00>{number = 5, name = (null)}
网络请求:3 ---- <NSThread: 0x600001b55780>{number = 4, name = (null)}
网络请求:1 ---- <NSThread: 0x600001b550c0>{number = 7, name = (null)}
网络请求:4 ---- <NSThread: 0x600001b521c0>{number = 3, name = (null)}
刷新UI ---- <NSThread: 0x600001b521c0>{number = 3, name = (null)}
  • dispatch_barrier_sync
    dispatch_queue_t concurrentQueue = dispatch_queue_create("groupqueue", DISPATCH_QUEUE_CONCURRENT);
    
    for (NSInteger i = 0; i < 5; i++) {
        dispatch_async(concurrentQueue, ^{
            [NSThread sleepForTimeInterval:1];
            NSLog(@"网络请求:%ld ---- %@",i,[NSThread currentThread]);
        });
    }
    
    dispatch_barrier_sync(concurrentQueue, ^{
        NSLog(@"刷新UI ---- %@",[NSThread currentThread]);
    });
    NSLog(@"end");
网络请求:3 ---- <NSThread: 0x600002782380>{number = 8, name = (null)}
网络请求:0 ---- <NSThread: 0x6000027805c0>{number = 7, name = (null)}
网络请求:4 ---- <NSThread: 0x600002782580>{number = 5, name = (null)}
网络请求:2 ---- <NSThread: 0x6000027c9380>{number = 6, name = (null)}
网络请求:1 ---- <NSThread: 0x60000279c500>{number = 4, name = (null)}
刷新UI ---- <NSThread: 0x6000027cca00>{number = 1, name = main}
end

参考

2019 iOS面试题-----多线程相关
阿里、字节:一套高效的iOS面试题(八 - 多线程 GCD)

相关文章

  • GCD相关题目

    1、以下代码结果会如何? 结果如下: 会造成死锁,主线程中【同步执行+主队列】,造成的互相等待。 2、写一个线程安...

  • GCD相关

    本文是自己写的总结GCD的Demo的结果。总结的过程中,参考了很多文章,文章底部有引用链接,在此感谢。多图,流量慎...

  • GCD相关

    运行结果 或者 或者 结语:dispatch_async 直接返回,具体执行顺序不确定。 运行结果 卡死、系统报错...

  • GCD相关

    五种优先级的不同使用场景 创建指定优先级的串行队列的方式: 以上内容参考链接 YYKit学习笔记

  • GCD相关

    创建队列 dispatch_queue_create("我是串行队列",DISPATCH_QUEUE_SERIAL...

  • GCD相关

    关于GCD我用的并不多,都是最基本的,具体如下:1.获取主线程,并执行: 注意,不要在主线程同步执行主线程,会崩溃...

  • OC多线程学习(二) - GCD

    本文内容: GCD相关概念 有关GCD的几道面试题 源码分析:队列和异步函数 GCD概念 GCD是Grand Ce...

  • 模板-->单变元模线性方程

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板...

  • 模板-->中国剩余定理[互质版本]

    如果有相应的OJ题目,欢迎同学们提供相应的链接 相关链接 所有模板的快速链接 扩展欧几里得extend_gcd模板...

  • 多线程相关

    引文: 多线程相关 OC 语言相关 内存管理相关 UI视图相关 RunLoop相关 GCD NSOperation...

网友评论

      本文标题:GCD相关题目

      本文链接:https://www.haomeiwen.com/subject/zwmosltx.html