GCD QUEUE

同步串行队列、异步串行队列和同步并行队列都只创建一个线程,而异步并行队列则创建多个线程。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
dispatch_queue_t queue1 = dispatch_queue_create("com.demo.1", DISPATCH_QUEUE_SERIAL);
for (int i = 0; i < 10; ++i) {
// dispatch_sync(queue1, ^{
// NSLog(@"%p", [NSThread currentThread]);
// });
/*
2018-09-17 18:38:38.654309+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654448+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654543+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654643+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654729+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654879+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.654974+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.655165+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.655250+0800 testUI[31197:422120] 0x60400006ac80
2018-09-17 18:38:38.655363+0800 testUI[31197:422120] 0x60400006ac80
*/
// dispatch_async(queue1, ^{
// NSLog(@"%p", [NSThread currentThread]);
// });
/*
2018-09-17 18:40:02.443306+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.443462+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.443560+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.443660+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.443830+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.444017+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.444403+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.444509+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.444737+0800 testUI[31225:423440] 0x604000269740
2018-09-17 18:40:02.445109+0800 testUI[31225:423440] 0x604000269740
*/
}
dispatch_queue_t queue2 = dispatch_queue_create("com.demo.2", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 10; ++i) {
// dispatch_sync(queue2, ^{
// NSLog(@"%p", [NSThread currentThread]);
// });
/*
2018-09-17 18:41:26.184301+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184430+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184519+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184612+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184721+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184807+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.184906+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.185005+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.185111+0800 testUI[31260:424690] 0x600000063540
2018-09-17 18:41:26.185218+0800 testUI[31260:424690] 0x600000063540
*/
dispatch_async(queue2, ^{
NSLog(@"%p", [NSThread currentThread]);
});
/*
2018-09-17 18:43:35.575104+0800 testUI[31313:426896] 0x604000469f40
2018-09-17 18:43:35.575104+0800 testUI[31313:426894] 0x600000271cc0
2018-09-17 18:43:35.575104+0800 testUI[31313:426895] 0x600000271bc0
2018-09-17 18:43:35.575126+0800 testUI[31313:426893] 0x600000272480
2018-09-17 18:43:35.575260+0800 testUI[31313:426894] 0x600000271cc0
2018-09-17 18:43:35.575270+0800 testUI[31313:426896] 0x604000469f40
2018-09-17 18:43:35.575304+0800 testUI[31313:426895] 0x600000271bc0
2018-09-17 18:43:35.575359+0800 testUI[31313:426893] 0x600000272480
2018-09-17 18:43:35.575386+0800 testUI[31313:426897] 0x600000271c40
2018-09-17 18:43:35.575394+0800 testUI[31313:426894] 0x600000271cc0
*/
}

最后一个结果有点意思,事实上没有创建10个线程,推测应该是此时GCD实现线程池的能力,以减少创建线程的开销,提高性能。

-------------本文结束感谢您的阅读-------------