面试精品iOS笔试面试iOS_bookmark

GCD的Dispatch Queue(串行队列、并发队列、主队列

2016-12-17  本文已影响2553人  KardelShaw

首先感谢行走的少年郎写的一篇文章《iOS多线程--彻底学会多线程之『GCD』》
它让我对GCD的许多概念有了清晰的理解。现在打算自己再亲自写一份笔记,以助加深印象。

1、队列

只有2种�队列:

创建串行队列:

dispatch_queue_t serialQueue = dispatch_queue_create("自定义署名", NULL);
//或
dispatch_queue_t serialQueue = dispatch_queue_create("自定义署名", DISPATCH_QUQUE_SERIAL);

创建并发队列:

dispatch_queue_t concurrentQueue = dispatch_queue_create("自定义署名", DISPATCH_QUQUE_CONCURRENT);

2、任务

只有2种执行任务的方式:

同步执行:

dispatch_sync(queue, ^{  ...  });

异步执行:

dispatch_async(queue, ^{ ... });

还有一点区别就是:
同步队列是把一个任务添加到队列后立马就执行;而并发队列不会。
这会在4、GCD的具体使用中得到体现。

3、使用GCD就是队列+任务

补充说明:主队列属于串行队列,但因其特殊性单独列出来。

4、GCD的具体使用

(1)串行队列+同步执行

- (void) KSserialQueueSync {
    NSLog(@"test start");
    
    dispatch_queue_t serialQueue = dispatch_queue_create("com.ks.serialQueue", NULL);
    
    dispatch_sync(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[915:40526] test start
[915:40526] block1 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] block1 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] block2 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] block2 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] block3 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] block3 <NSThread: 0x610000069e80>{number = 1, name = main}
[915:40526] test over

输出结果分析:

(2)串行队列+异步执行

- (void)KSserialQueueAsync {
    NSLog(@"test start");
    
    dispatch_queue_t serialQueue = dispatch_queue_create("com.ks.serialQueue", NULL);
    
    dispatch_async(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(serialQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[941:46623] test start
[941:46623] test over
[941:46686] block1 <NSThread: 0x61800007a100>{number = 2, name = (null)}
[941:46686] block1 <NSThread: 0x61800007a100>{number = 2, name = (null)}
[941:46686] block2 <NSThread: 0x61800007a100>{number = 2, name = (null)}
[941:46686] block2 <NSThread: 0x61800007a100>{number = 2, name = (null)}
[941:46686] block3 <NSThread: 0x61800007a100>{number = 2, name = (null)}
[941:46686] block3 <NSThread: 0x61800007a100>{number = 2, name = (null)}

输出结果分析:

(3)并发队列+同步执行

- (void)KSconcurrentQueueSync {
    NSLog(@"test start");
    
    dispatch_queue_t concurrentQueue = dispatch_queue_create("com.ks.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_sync(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[1001:58076] test start
[1001:58076] block1 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] block1 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] block2 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] block2 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] block3 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] block3 <NSThread: 0x60000006da80>{number = 1, name = main}
[1001:58076] test over

输出结果分析:

(4)并发队列+异步执行

- (void)KSconcurrentQueueAsync {
    NSLog(@"test start");
    
    dispatch_queue_t concurrentQueue = dispatch_queue_create("com.ks.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(concurrentQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[1042:64557] test start
[1042:64557] test over
[1042:64615] block3 <NSThread: 0x610000067c00>{number = 4, name = (null)}
[1042:64618] block2 <NSThread: 0x608000067e80>{number = 3, name = (null)}
[1042:64640] block1 <NSThread: 0x610000067d00>{number = 2, name = (null)}
[1042:64615] block3 <NSThread: 0x610000067c00>{number = 4, name = (null)}
[1042:64618] block2 <NSThread: 0x608000067e80>{number = 3, name = (null)}
[1042:64640] block1 <NSThread: 0x610000067d00>{number = 2, name = (null)}

输出结果分析:

(5)主队列+同步执行

- (void)KSmainQueueSync {
    NSLog(@"test start");
    
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_sync(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_sync(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[1084:70872] test start

输出结果分析:

只要在另一条线程上调用就好了,利用串行队列+异步执行来创建一条新的线程

dispatch_queue_t queue = dispatch_queue_create("com.ks.serialQueue", NULL);
    
dispatch_async(queue, ^{
        [self KSmainQueueSync];
});
[1097:77638] test start
[1097:77593] block1 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77593] block1 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77593] block2 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77593] block2 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77593] block3 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77593] block3 <NSThread: 0x6080000699c0>{number = 1, name = main}
[1097:77638] test over

输出结果分析:主队列是串行队列的一种,所以之前对串行队列的分析这里也适用

(6)主队列+异步执行

- (void)KSmainQueueAsync {
    NSLog(@"test start");
    
    dispatch_queue_t mainQueue = dispatch_get_main_queue();
    
    dispatch_async(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block1 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block2 %@", [NSThread currentThread]);
        }
    });
    
    dispatch_async(mainQueue, ^{
        for (int i = 0; i < 2; i++) {
            NSLog(@"block3 %@", [NSThread currentThread]);
        }
    });
    
    NSLog(@"test over");
}
[1130:84261] test start
[1130:84261] test over
[1130:84261] block1 <NSThread: 0x60800006de40>{number = 1, name = main}
[1130:84261] block1 <NSThread: 0x60800006de40>{number = 1, name = main}
[1130:84261] block2 <NSThread: 0x60800006de40>{number = 1, name = main}
[1130:84261] block2 <NSThread: 0x60800006de40>{number = 1, name = main}
[1130:84261] block3 <NSThread: 0x60800006de40>{number = 1, name = main}
[1130:84261] block3 <NSThread: 0x60800006de40>{number = 1, name = main}

输出结果分析:

</br>
�归纳注意事项:
(1)创建一个队列与创建一个线程是不同的两件事。
(2)在一个线程内可能会有多个队列,混杂有串行队列和并行队列。
(3)是否创建新线程,取决于队列是同步执行还是异步执行。
(4)死锁问题。

上一篇下一篇

猜你喜欢

热点阅读