iOS多线程面试题-启动三个线程A,B,C,打印10次 按照ABC的顺序输出(异步转同步)

题目:
启动三个线程A,B,C,打印10次 按照ABC的顺序输出

1. OC 使用NSLock


    NSLock *lockA = [[NSLock alloc] init];
    NSLock *lockB = [[NSLock alloc] init];
    NSLock *lockC = [[NSLock alloc] init];
    
    [lockB lock];
    [lockC lock];
    
    dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueA, ^{
        for (int i = 0; i<10; i++) {
            [lockA lock];
            NSLog(@"A======= %@",@(i));
            [lockB unlock];
        }
    });
    
    dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueB, ^{
        for (int i = 0; i<10; i++) {
            [lockB lock];
             NSLog(@"B======= %@",@(i));
            [lockC unlock];
        }
    });
    
    dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueC, ^{
        for (int i = 0; i<10; i++) {
            [lockC lock];
             NSLog(@"C======= %@",@(i));
             NSLog(@"   ");
            [lockA unlock];
        }
    });

输出:

2019-06-20 01:06:23.384170+0800 xxtest[2808:669702] A======= 0
2019-06-20 01:06:23.384379+0800 xxtest[2808:669701] B======= 0
2019-06-20 01:06:23.384515+0800 xxtest[2808:669703] C======= 0
2019-06-20 01:06:23.384613+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.384750+0800 xxtest[2808:669702] A======= 1
2019-06-20 01:06:23.384878+0800 xxtest[2808:669701] B======= 1
2019-06-20 01:06:23.384992+0800 xxtest[2808:669703] C======= 1
2019-06-20 01:06:23.385716+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.386194+0800 xxtest[2808:669702] A======= 2
2019-06-20 01:06:23.386590+0800 xxtest[2808:669701] B======= 2
2019-06-20 01:06:23.387100+0800 xxtest[2808:669703] C======= 2
2019-06-20 01:06:23.387497+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.387857+0800 xxtest[2808:669702] A======= 3
2019-06-20 01:06:23.388317+0800 xxtest[2808:669701] B======= 3
2019-06-20 01:06:23.388663+0800 xxtest[2808:669703] C======= 3
2019-06-20 01:06:23.388969+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.389857+0800 xxtest[2808:669702] A======= 4
2019-06-20 01:06:23.390137+0800 xxtest[2808:669701] B======= 4
2019-06-20 01:06:23.390546+0800 xxtest[2808:669703] C======= 4
2019-06-20 01:06:23.390847+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.391203+0800 xxtest[2808:669702] A======= 5
2019-06-20 01:06:23.391476+0800 xxtest[2808:669701] B======= 5
2019-06-20 01:06:23.391854+0800 xxtest[2808:669703] C======= 5
2019-06-20 01:06:23.392134+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.392443+0800 xxtest[2808:669702] A======= 6
2019-06-20 01:06:23.392804+0800 xxtest[2808:669701] B======= 6
2019-06-20 01:06:23.393107+0800 xxtest[2808:669703] C======= 6
2019-06-20 01:06:23.393413+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.393685+0800 xxtest[2808:669702] A======= 7
2019-06-20 01:06:23.394006+0800 xxtest[2808:669701] B======= 7
2019-06-20 01:06:23.394301+0800 xxtest[2808:669703] C======= 7
2019-06-20 01:06:23.394578+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.394959+0800 xxtest[2808:669702] A======= 8
2019-06-20 01:06:23.395532+0800 xxtest[2808:669701] B======= 8
2019-06-20 01:06:23.395873+0800 xxtest[2808:669703] C======= 8
2019-06-20 01:06:23.396117+0800 xxtest[2808:669703] 
2019-06-20 01:06:23.396508+0800 xxtest[2808:669702] A======= 9
2019-06-20 01:06:23.396897+0800 xxtest[2808:669701] B======= 9
2019-06-20 01:06:23.397307+0800 xxtest[2808:669703] C======= 9
2019-06-20 01:06:23.397649+0800 xxtest[2808:669703] 

2. OC 使用dispatch_semaphore

 dispatch_semaphore_t semaA = dispatch_semaphore_create(1);
    dispatch_semaphore_t semaB = dispatch_semaphore_create(0);
    dispatch_semaphore_t semaC = dispatch_semaphore_create(0);
    
    
    dispatch_queue_t queueA = dispatch_queue_create("queuea", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueA, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaA, DISPATCH_TIME_FOREVER);
            NSLog(@"A======= %@",@(i));
            dispatch_semaphore_signal(semaB);
        }
    });
    
    dispatch_queue_t queueB = dispatch_queue_create("queueb", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueB, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaB, DISPATCH_TIME_FOREVER);
            NSLog(@"B======= %@",@(i));
            dispatch_semaphore_signal(semaC);
        }
    });
    
    dispatch_queue_t queueC = dispatch_queue_create("queuec", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(queueC, ^{
        for (int i = 0; i<10; i++) {
            dispatch_semaphore_wait(semaC, DISPATCH_TIME_FOREVER);
            NSLog(@"C======= %@",@(i));
            NSLog(@"   ");
            dispatch_semaphore_signal(semaA);
        }
    });
    

输出:

2019-06-20 01:40:25.050013+0800 xxtest[2938:708288] A======= 0
2019-06-20 01:40:25.050183+0800 xxtest[2938:708285] B======= 0
2019-06-20 01:40:25.050297+0800 xxtest[2938:708286] C======= 0
2019-06-20 01:40:25.050391+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.050487+0800 xxtest[2938:708288] A======= 1
2019-06-20 01:40:25.050588+0800 xxtest[2938:708285] B======= 1
2019-06-20 01:40:25.050684+0800 xxtest[2938:708286] C======= 1
2019-06-20 01:40:25.050782+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.050881+0800 xxtest[2938:708288] A======= 2
2019-06-20 01:40:25.050982+0800 xxtest[2938:708285] B======= 2
2019-06-20 01:40:25.051442+0800 xxtest[2938:708286] C======= 2
2019-06-20 01:40:25.051695+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.051928+0800 xxtest[2938:708288] A======= 3
2019-06-20 01:40:25.052162+0800 xxtest[2938:708285] B======= 3
2019-06-20 01:40:25.052414+0800 xxtest[2938:708286] C======= 3
2019-06-20 01:40:25.052841+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.053073+0800 xxtest[2938:708288] A======= 4
2019-06-20 01:40:25.053340+0800 xxtest[2938:708285] B======= 4
2019-06-20 01:40:25.053617+0800 xxtest[2938:708286] C======= 4
2019-06-20 01:40:25.053843+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.054094+0800 xxtest[2938:708288] A======= 5
2019-06-20 01:40:25.055448+0800 xxtest[2938:708285] B======= 5
2019-06-20 01:40:25.055584+0800 xxtest[2938:708286] C======= 5
2019-06-20 01:40:25.055757+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.055888+0800 xxtest[2938:708288] A======= 6
2019-06-20 01:40:25.056116+0800 xxtest[2938:708285] B======= 6
2019-06-20 01:40:25.056455+0800 xxtest[2938:708286] C======= 6
2019-06-20 01:40:25.056871+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.057176+0800 xxtest[2938:708288] A======= 7
2019-06-20 01:40:25.057484+0800 xxtest[2938:708285] B======= 7
2019-06-20 01:40:25.057789+0800 xxtest[2938:708286] C======= 7
2019-06-20 01:40:25.058204+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.058479+0800 xxtest[2938:708288] A======= 8
2019-06-20 01:40:25.058947+0800 xxtest[2938:708285] B======= 8
2019-06-20 01:40:25.059211+0800 xxtest[2938:708286] C======= 8
2019-06-20 01:40:25.059469+0800 xxtest[2938:708286] 
2019-06-20 01:40:25.059701+0800 xxtest[2938:708288] A======= 9
2019-06-20 01:40:25.060025+0800 xxtest[2938:708285] B======= 9
2019-06-20 01:40:25.060360+0800 xxtest[2938:708286] C======= 9
2019-06-20 01:40:25.060585+0800 xxtest[2938:708286] 

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 11,903评论 1赞 32
  • 本文用来介绍 iOS 多线程中 GCD 的相关知识以及使用方法。这大概是史上最详细、清晰的关于 GCD 的详细讲...
    花花世界的孤独行者阅读 645评论 0赞 1
  • iOS多线程编程 基本知识 1. 进程(process) 进程是指在系统中正在运行的一个应用程序,就是一段程序的执...
    陵无山阅读 6,416评论 1赞 14
  • 安迪,成功女性的代表。学历高,情商高,看似高冷,但内心善良,每当姐妹们遇到事情向她诉说时,她都会认真分析并且给出发...
    爱jian阅读 198评论 0赞 0
  • 有时会发现人和人之间真的有距离,而这种距离并不是离得远近的距离,而是灵魂和认知的距离!
    幸福妈妈雨后阳光阅读 157评论 0赞 0

友情链接更多精彩内容