美文网首页
iOS多线程面试题-启动三个线程A,B,C,打印10次 按照AB

iOS多线程面试题-启动三个线程A,B,C,打印10次 按照AB

作者: Pandakingli | 来源:发表于2019-06-20 01:44 被阅读0次

    题目:
    启动三个线程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] 
    
    

    相关文章

      网友评论

          本文标题:iOS多线程面试题-启动三个线程A,B,C,打印10次 按照AB

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