dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----- %@",[NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier函数l开始");
NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");
执行结束
download1-----
download3-----
download2-----
barrier函数l开始
barrier函数-----
download4-----
dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----- %@",[NSThread currentThread]);
});
dispatch_barrier_sync(queue, ^{
NSLog(@"barrier函数l开始");
NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");
download3-----
download1-----
download2-----
barrier函数l开始
barrier函数-----
执行结束
download4-----
dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"延时函数----- %@",[NSThread currentThread]);
});
NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----- %@",[NSThread currentThread]);
});
dispatch_barrier_sync(queue, ^{
NSLog(@"barrier函数l开始");
NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");
download1-----
download2-----
download3-----
barrier函数l开始
barrier函数-----
执行结束
download4-----
延时函数-----
dispatch_queue_t queue = dispatch_queue_create("com.ccxd.download", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"download1----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"延时函数----- %@",[NSThread currentThread]);
});
NSLog(@"download2----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download3----- %@",[NSThread currentThread]);
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier函数l开始");
NSLog(@"barrier函数----- %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"download4----- %@",[NSThread currentThread]);
});
NSLog(@"执行结束");
执行结束
download1-----
download2-----
download3-----
barrier函数l开始
barrier函数-----
download4-----
延时函数-----
dispatch_barrier_async 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,不会阻塞主线程
dispatch_barrier_sync 的作用是等待队列的前面的任务执行完毕后,才执行dispatch_barrier_async的block里面的任务,阻塞主线程
当dispatch_barrier_async和dispatch_barrier_sync的队列里面有异步的任务的时候,dispatch_barrier不会等到异步的任务执行完成才执行。