一、工作原理
让程序平行排队的特定任务,根据可用的处理资源,安排其在任何可用的处理器和欣赏执行任务。
可以是block 或者是函数(function)。
二、工作方式
1、最常用的方式 dispatch_async 异步线程处理方式
a. 好处:(1)异步线程操作,避免了在处理耗时操作时ui卡死
(2)GCD在进行任务处理时会根据任务在处理器上进行资源分配,优化程序
(3)不需要关心内存释放问题
(4)在线程中处理完之后,再通过主线程进行UI更新
b.常用于:网络数据读取、数据库读写
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),
^{
[NSThreadsleepForTimeInterval:1.0];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"async done");
});
});
[NSThreadsleepForTimeInterval:1.0];
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"async done");
});
});
NSLog(@"124");
2、dispatch_group_async 使用
a.功能点:监听一组任务是否完成,在所有任务完成后通知线程进行其他操作
// dispatch_group_async
dispatch_queue_t queue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:1];
NSLog(@"first async");
});
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:2];
NSLog(@"second async");
});
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:3];
NSLog(@"third async");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"group done");
dispatch_queue_t queue = dispatch_queue_create(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:1];
NSLog(@"first async");
});
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:2];
NSLog(@"second async");
});
dispatch_group_async(group, queue, ^{
[NSThreadsleepForTimeInterval:3];
NSLog(@"third async");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"group done");
});
3、dispatch_barrier_async 使用
a. 在前面的任务执行结束后执行,后面的任务需要等它结束后才会执行
// dispatch_barrier_asnyc
dispatch_queue_t queue = dispatch_queue_create("dispatch_barrier_asnyc",0);
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:3.0f];
NSLog(@"first run");
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:7.0f];
NSLog(@"second run");
});
dispatch_barrier_async(queue, ^{
NSLog(@"dispatch_barrier_async");
[NSThreadsleepForTimeInterval:1.0f];
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:1.0f];
NSLog(@"third run");
dispatch_queue_t queue = dispatch_queue_create("dispatch_barrier_asnyc",0);
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:3.0f];
NSLog(@"first run");
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:7.0f];
NSLog(@"second run");
});
dispatch_barrier_async(queue, ^{
NSLog(@"dispatch_barrier_async");
[NSThreadsleepForTimeInterval:1.0f];
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:1.0f];
NSLog(@"third run");
});
2015-04-09 11:43:51.863 [14074:522786] first run
2015-04-09 11:43:58.865 [14074:522786] second run
2015-04-09 11:43:58.865 [14074:522786] dispatch_barrier_async
2015-04-09 11:44:00.867 [14074:522786] third run
4、dispatch_apply 执行某个代码片段N次
注:该方法主要用于异步线程当中的子线程,在主线程中没有任何响应
// 多次执行某个代码片段
dispatch_apply(5,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(size_t index){
NSLog(@"%zu",index);
dispatch_apply(5,dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^(size_t index){
NSLog(@"%zu",index);
});
5、dispatch_after 延迟执行一些代码
// 延迟方法执行
NSLog(@"time");
double delayTime = 3.0f;
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)delayTime * NSEC_PER_SEC);
dispatch_after(time,dispatch_get_main_queue(), ^{
NSLog(@"延迟方法执行");
NSLog(@"time");
double delayTime = 3.0f;
dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, (int64_t)delayTime * NSEC_PER_SEC);
dispatch_after(time,dispatch_get_main_queue(), ^{
NSLog(@"延迟方法执行");
});
6、dispatch_once 只执行一次,用于单例
// 单例方式
staticdispatch_once_t once;
dispatch_once(&once, ^{
// 只执行一次
staticdispatch_once_t once;
dispatch_once(&once, ^{
// 只执行一次
});
本文详细介绍了Grand Central Dispatch (GCD) 的核心概念及其在并发编程中的应用,包括异步处理、任务组、屏障同步、代码片段重复执行、延迟执行及确保单一执行等功能。文章通过具体示例解释了如何利用GCD提高程序效率并简化多线程编程。
744

被折叠的 条评论
为什么被折叠?



