iOS GCD不同场景的使用比較
/**
* async -- 并发队列
* 会创建线程。一般同一时候开多条
* 并发运行任务
*/
<span style="font-size:14px;">- (void)asyncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 将 任务 加入 全局队列 中去 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
</span><span style="font-size:14px;">-----下载图片4---<NSThread: 0x7fbd52c9b390>{number = 5, name = (null)}
-----下载图片3---<NSThread: 0x7fbd52f00230>{number = 4, name = (null)}
-----下载图片2---<NSThread: 0x7fbd52e3eed0>{number = 3, name = (null)}
-----下载图片5---<NSThread: 0x7fbd52e2eb40>{number = 6, name = (null)}
-----下载图片1---<NSThread: 0x7fbd52c06010>{number = 2, name = (null)}</span>
/**
* async -- 串行队列
* 会创建线程, 一般仅仅开1条线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
*/
- (void)asyncSerialQueue
{
// 1.创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);
// 2.将任务加入到串行队列中 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片2---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片3---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片4---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
-----下载图片5---<NSThread: 0x7fd4c5b00b50>{number = 2, name = (null)}
/**
* async -- 主队列
*/
- (void)asyncMainQueue
{
// 1.主队列(加入到主队列中的任务。都会自己主动放到主线程中去运行)
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.加入 任务 到主队列中 异步 运行
dispatch_async(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}
-----下载图片1---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7faaca905000>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7faaca905000>{number = 1, name = main}
/**
* sync -- 主队列(会卡死)
*/
- (void)syncMainQueue
{
NSLog(@"syncMainQueue----begin--");
// 1.主队列(加入到主队列中的任务,都会自己主动放到主线程中去运行)
dispatch_queue_t queue = dispatch_get_main_queue();
// 2.加入 任务 到主队列中 异步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
NSLog(@"syncMainQueue----end--");
}syncMainQueue----begin--
/**
* sync -- 并发队列
* 不会创建线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
* 并发队列失去了并发的功能
*/
- (void)syncGlobalQueue
{
// 获得全局的并发队列
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 将 任务 加入到 全局并发队列 中 同步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}-----下载图片1---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fe5b2904c30>{number = 1, name = main}
/**
* sync -- 串行队列
* 不会创建线程
* 串行运行任务(一个任务运行完成后再运行下一个任务)
*/
- (void)syncSerialQueue
{
// 创建一个串行队列
dispatch_queue_t queue = dispatch_queue_create("cn.heima.queue", NULL);
// 将 任务 加入到 串行队列 中 同步 运行
dispatch_sync(queue, ^{
NSLog(@"-----下载图片1---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片2---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片3---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片4---%@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"-----下载图片5---%@", [NSThread currentThread]);
});
}-----下载图片1---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片2---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片3---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片4---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
-----下载图片5---<NSThread: 0x7fdb40f05020>{number = 1, name = main}
浙公网安备 33010602011771号