GCD 队列笔记
2020-03-04 本文已影响0人
像天空的鸽子
// 异步函数 + 主队列
// 不会开启子线程 , 所有的任务在主线程中串行执行
- (void)async_main
{
//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]);
});
dispatch_async(queue, ^{
NSLog(@"6----%@",[NSThread currentThread]);
});
}
// 同步函数 + 串行队列
// 不会开启子线程 , 所有的任务在当前线程中串行执行
- (void)sync_serial
{
//1.创建队列
dispatch_queue_t queue = dispatch_queue_create("com.ylink.www", DISPATCH_QUEUE_SERIAL);
//2.封装任务,把任务添加到队列
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]);
});
dispatch_sync(queue, ^{
NSLog(@"6----%@",[NSThread currentThread]);
});
}
// 同步函数 + 并发队列
// 不会创建子线程 , 所有任务都在(注意:当前)线程中串行执行
- (void)sync_Concurrent
{
//1.创建队列
dispatch_queue_t queue = dispatch_queue_create("com.ylink.www", DISPATCH_QUEUE_CONCURRENT);
//2.封装任务,把任务添加到队列
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]);
});
dispatch_sync(queue, ^{
NSLog(@"6----%@",[NSThread currentThread]);
});
}
// 异步函数 + 串行队列
// 会创建一个子线程 , 所有任务都在该子线程中串行执行
- (void)asyn_serial
{
//1.创建队列
dispatch_queue_t queue = dispatch_queue_create("com.ylink.www", DISPATCH_QUEUE_SERIAL);
//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]);
});
dispatch_async(queue, ^{
NSLog(@"6----%@",[NSThread currentThread]);
});
}
// 异步函数 + 并发队列
// 创建的线程数不一定完全跟任务数一样多 , 线程数量系统自动决定
- (void)asy_Concurrent
{
//1.创建并发队列
dispatch_queue_t queue = dispatch_queue_create("com.ylink.www", DISPATCH_QUEUE_CONCURRENT);
//2.封装任务,把任务添加到队列
dispatch_async(queue, ^{
NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3----%@",[NSThread currentThread]);
});
}
快速迭代函数:
用的时候永远传并发队列就行
使用方法
栅栏函数:
队列组:
等所有的并发任务执行完才执行xxx任务
单个队列
多个队列
多个队列
老用法(不推荐):
老办法下载两张图片,下载完之后合成一张图片.
对应的函数方法:
区别:
一个任务通过block执行
一个任务通过函数执行
GCD注意事项: