GCD研究
//创建gcd容器(全局队列)并行
dispatch_queue_t queue0 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t queue1 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
1这部分代码证明在同一个全局队列里异步加载不同的任务,执行是并发的(兄弟之间,你一下我一下的感觉
)
#pragma mark--1.这部分代码证明在同一个全局队列里加载不同的任务,执行是并发的
dispatch_async(queue0, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"i = %d", i);
}
});
dispatch_async(queue0, ^{
for (int j = 20; j < 30; j++) {
NSLog(@"j = %d", j);
}
});
执行结果
2016-01-07 22:36:51.430 gcd研究[1694:45405] i = 0
2016-01-07 22:36:51.430 gcd研究[1694:45406] j = 20
2016-01-07 22:36:51.430 gcd研究[1694:45405] i = 1
2016-01-07 22:36:51.430 gcd研究[1694:45406] j = 21
2016-01-07 22:36:51.430 gcd研究[1694:45405] i = 2
2016-01-07 22:36:51.430 gcd研究[1694:45406] j = 22
2016-01-07 22:36:51.430 gcd研究[1694:45405] i = 3
2016-01-07 22:36:51.430 gcd研究[1694:45406] j = 23
2016-01-07 22:36:51.431 gcd研究[1694:45405] i = 4
2016-01-07 22:36:51.431 gcd研究[1694:45406] j = 24
2016-01-07 22:36:51.431 gcd研究[1694:45405] i = 5
2016-01-07 22:36:51.431 gcd研究[1694:45406] j = 25
2016-01-07 22:36:51.431 gcd研究[1694:45405] i = 6
2016-01-07 22:36:51.431 gcd研究[1694:45406] j = 26
2016-01-07 22:36:51.431 gcd研究[1694:45405] i = 7
2016-01-07 22:36:51.431 gcd研究[1694:45406] j = 27
2016-01-07 22:36:51.431 gcd研究[1694:45405] i = 8
2016-01-07 22:36:51.431 gcd研究[1694:45406] j = 28
2016-01-07 22:36:51.432 gcd研究[1694:45405] i = 9
2016-01-07 22:36:51.432 gcd研究[1694:45406] j = 29
2这部分代码证明在同一个全局队列里同步步加载不同的任务,执行是同步的
dispatch_sync(queue0, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"i = %d", i);
}
});
dispatch_sync(queue0, ^{
for (int j = 20; j < 30; j++) {
NSLog(@"j = %d", j);
}
});
全局队列 同步执行结果
2016-01-07 22:40:02.903 gcd研究[1745:47470] i = 0
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 1
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 2
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 3
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 4
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 5
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 6
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 7
2016-01-07 22:40:02.904 gcd研究[1745:47470] i = 8
2016-01-07 22:40:02.905 gcd研究[1745:47470] i = 9
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 20
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 21
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 22
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 23
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 24
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 25
2016-01-07 22:40:02.905 gcd研究[1745:47470] j = 26
2016-01-07 22:40:02.906 gcd研究[1745:47470] j = 27
2016-01-07 22:40:02.906 gcd研究[1745:47470] j = 28
2016-01-07 22:40:02.906 gcd研究[1745:47470] j = 29
3这样看似在全局队列里加载两个任务,其实只是一个任务,会按照函数执行顺序执行
dispatch_async(queue0, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"i = %d", i);
}
for (int j = 20; j < 30; j++) {
NSLog(@"j = %d", j);
}
});
执行结果
2016-01-07 22:46:27.088 gcd研究[1847:51079] i = 0
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 1
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 2
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 3
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 4
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 5
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 6
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 7
2016-01-07 22:46:27.089 gcd研究[1847:51079] i = 8
2016-01-07 22:46:27.090 gcd研究[1847:51079] i = 9
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 20
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 21
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 22
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 23
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 24
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 25
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 26
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 27
2016-01-07 22:46:27.090 gcd研究[1847:51079] j = 28
2016-01-07 22:46:27.091 gcd研究[1847:51079] j = 29
4gcd添加两个不同的全局队列,并发运行(完全没有先后可言)
dispatch_async(queue0, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"i = %d", i);
}
});
dispatch_async(queue1, ^{
for (int j = 20; j < 30; j++) {
NSLog(@"j = %d", j);
}
});
执行结果如下
2016-01-07 22:53:13.482 gcd研究[1961:55603] i = 0
2016-01-07 22:53:13.482 gcd研究[1961:55603] i = 1
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 2
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 3
2016-01-07 22:53:13.482 gcd研究[1961:55604] j = 20
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 4
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 5
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 6
2016-01-07 22:53:13.483 gcd研究[1961:55604] j = 21
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 7
2016-01-07 22:53:13.483 gcd研究[1961:55603] i = 8
2016-01-07 22:53:13.483 gcd研究[1961:55604] j = 22
2016-01-07 22:53:13.484 gcd研究[1961:55603] i = 9
2016-01-07 22:53:13.484 gcd研究[1961:55604] j = 23
2016-01-07 22:53:13.484 gcd研究[1961:55604] j = 24
2016-01-07 22:53:13.485 gcd研究[1961:55604] j = 25
2016-01-07 22:53:13.493 gcd研究[1961:55604] j = 26
2016-01-07 22:53:13.493 gcd研究[1961:55604] j = 27
2016-01-07 22:53:13.493 gcd研究[1961:55604] j = 28
2016-01-07 22:53:13.494 gcd研究[1961:55604] j = 29
5以上俩个结合
dispatch_queue_t queue3 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_async(queue3, ^{
for (int i = 0; i < 10; i++) {
NSLog(@"what a fun day");
}
});
dispatch_async(queue3, ^{
for (int i= 1; i < 10; i++) {
NSLog(@"%d", i);
}
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"It's main queue, my clood");
});
});
执行结果
2016-01-07 22:59:03.857 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.857 gcd研究[2040:59312] 1
2016-01-07 22:59:03.858 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.858 gcd研究[2040:59312] 2
2016-01-07 22:59:03.858 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.858 gcd研究[2040:59312] 3
2016-01-07 22:59:03.858 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.858 gcd研究[2040:59312] 4
2016-01-07 22:59:03.858 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.858 gcd研究[2040:59312] 5
2016-01-07 22:59:03.858 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.858 gcd研究[2040:59312] 6
2016-01-07 22:59:03.859 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.859 gcd研究[2040:59312] 7
2016-01-07 22:59:03.859 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.859 gcd研究[2040:59312] 8
2016-01-07 22:59:03.859 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.859 gcd研究[2040:59312] 9
2016-01-07 22:59:03.859 gcd研究[2040:59262] what a fun day
2016-01-07 22:59:03.863 gcd研究[2040:59211] It's main queue, my clood
从上边的结果,理解,一个gcd块里对应的整体是一个任务,在这个块里是按顺序执行的,而多个gcd块里的任务可以在同一个gcd容器里,也可以说是同一个全局队列里,若这些个gcd块是async,那么执行起来这些gcd块就是异步的。若这些gcd是sync,那么执行起来这些gcd块就是同步的了。
如果两个gcd块在不同的容器异步执行里,即在不同的全局队列里,也是异步执行的。