dispatch_barrier_async
场景需求:需要异步完成三个任务。任务一、任务二、任务三。要求:任务三必须在任务一、任务二完成之后触发。这就需要使用dispatch_barrier_async。
特点:像一堵围墙、成为任务的分割线。
代码如下:
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//任务1
for (int i = 0; i < 2; i++) {
NSLog(@"我是任务一、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务2
for (int i = 0; i < 2 ; i++) {
NSLog(@"我是任务二、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_barrier_async(queue, ^{
//栅栏
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是分割线、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务3
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是任务三、来自线程:%@",[NSThread currentThread]);
}
});
复制代码
分析:任务一和任务二相互交错执行、由于栅栏函数存在、任务三最后执行。这三个任务都是异步完成、在各自的线程里。
dispatch_barrier_async 、dispatch_barrier_sync区别
直接看如下代码:
1.dispatch_barrier_async
NSLog(@"开始啦");
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//任务1
for (int i = 0; i < 2; i++) {
NSLog(@"我是任务一、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务2
for (int i = 0; i < 2 ; i++) {
NSLog(@"我是任务二、来自线程:%@",[NSThread currentThread]);
}
});
NSLog(@"动啊动啊");
dispatch_barrier_async(queue, ^{
//珊栏
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是分割线、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务3
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是任务三、来自线程:%@",[NSThread currentThread]);
}
});
NSLog(@"结束啦");
复制代码
打印结果:
2019-01-25 13:47:17.656139+0800 多线程demo[4912:51826] 开始啦
2019-01-25 13:47:17.656348+0800 多线程demo[4912:51826] 动啊动啊
2019-01-25 13:47:17.656463+0800 多线程demo[4912:51882] 我是任务一、来自线程:{number = 3, name = (null)}
2019-01-25 13:47:17.656470+0800 多线程demo[4912:51884] 我是任务二、来自线程:{number = 4, name = (null)}
2019-01-25 13:47:17.656599+0800 多线程demo[4912:51826] 结束啦
2019-01-25 13:47:17.656654+0800 多线程demo[4912:51884] 我是任务二、来自线程:{number = 4, name = (null)}
2019-01-25 13:47:17.656654+0800 多线程demo[4912:51882] 我是任务一、来自线程:{number = 3, name = (null)}
2019-01-25 13:47:17.657139+0800 多线程demo[4912:51882] 我是分割线、来自线程:{number = 3, name = (null)}
2019-01-25 13:47:17.657313+0800 多线程demo[4912:51882] 我是任务三、来自线程:{number = 3, name = (null)}
复制代码
分析:任务三确实在任务一、任务二之后完成。使用dispatch_barrier_async、体现了异步的特点、不做任何等待、直接返回。所以主线程中的打印、不会在栅栏函数后面。
2.dispatch_barrier_sync
代码如下:
NSLog(@"开始啦");
dispatch_queue_t queue = dispatch_queue_create("myQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
//任务1
for (int i = 0; i < 2; i++) {
NSLog(@"我是任务一、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务2
for (int i = 0; i < 2 ; i++) {
NSLog(@"我是任务二、来自线程:%@",[NSThread currentThread]);
}
});
NSLog(@"动次打次:%@",[NSThread currentThread]);
dispatch_barrier_sync(queue, ^{
//珊栏
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是分割线、来自线程:%@",[NSThread currentThread]);
}
});
dispatch_async(queue, ^{
//任务3
for (int i = 0; i < 1 ; i++) {
NSLog(@"我是任务三、来自线程:%@",[NSThread currentThread]);
}
});
NSLog(@"结束啦");
复制代码
打印结果:
2019-01-25 13:54:09.045223+0800 多线程demo[5014:57352] 开始啦
2019-01-25 13:54:09.045461+0800 多线程demo[5014:57352] 动次打次:{number = 1, name = main}
2019-01-25 13:54:09.045486+0800 多线程demo[5014:57400] 我是任务二、来自线程:{number = 4, name = (null)}
2019-01-25 13:54:09.045496+0800 多线程demo[5014:57404] 我是任务一、来自线程:{number = 3, name = (null)}
2019-01-25 13:54:09.045758+0800 多线程demo[5014:57400] 我是任务二、来自线程:{number = 4, name = (null)}
2019-01-25 13:54:09.045830+0800 多线程demo[5014:57404] 我是任务一、来自线程:{number = 3, name = (null)}
2019-01-25 13:54:09.045991+0800 多线程demo[5014:57352] 我是分割线、来自线程:{number = 1, name = main}
2019-01-25 13:54:09.046099+0800 多线程demo[5014:57352] 结束啦
2019-01-25 13:54:09.046124+0800 多线程demo[5014:57404] 我是任务三、来自线程:{number = 3, name = (null)}
复制代码
分析:确实分割了前后执行的任务