//同步: 不会开启新线程(特点:阻塞当前线程)
//异步: 具备开辟新线程的能力,但是并不一定开辟新线程(特点:不会阻塞当前线程)
//串行: 先进先出,必须完成上一个任务,才能执行下一个任务
//并发:
//同步和异步: 针对的是线程
//串行和并发: 针对的是队列
//多线程,会消耗CPU的资源: 一条子线程占512kb的内存,主线程占1M内存
//多线程原理: CPU在不同线程之间快速切换
//任务: block代码块
//死锁: 2个任务相互等待.你等我执行完,我等你执行完
//同步并发
- (void)syncConcurrent {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//异步并发
- (void)asyncConcurrent {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//异步串行
- (void)asyncSerial {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//同步串行
- (void)syncSerial {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//同步并发嵌套(同步)
- (void)syncConcurrent_sync {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//同步并发嵌套(异步)
- (void)syncConcurrent_async {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//同步串行嵌套(异步)
- (void)syncSerial_async {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);
dispatch_sync(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//异步串行嵌套(异步)
- (void)asyncSerial_async {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"任务1, %@", [NSThread currentThread]);
dispatch_async(queue, ^{
NSLog(@"任务2, %@", [NSThread currentThread]);
});
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
}
//异步串行嵌套(同步)--->死锁
- (void)asyncSerial_sync {
NSLog(@"begin");
dispatch_queue_t queue = dispatch_queue_create("com.gcd", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{//block1
NSLog(@"任务1, %@", [NSThread currentThread]);
dispatch_sync(queue, ^{//block2
NSLog(@"任务2, %@", [NSThread currentThread]);
});
NSLog(@"任务3, %@", [NSThread currentThread]);
});
NSLog(@"end");
//block1和block2两个任务死锁
}
//1、创建主线程(串行)
dispatch_async(dispatch_get_main_queue(), ^{
//刷新界面代码
});
//2、创建异步线程(并行)
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//比较耗时的代码放这里
});
//3、gcd延迟
double delayInSeconds = 1.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//延迟代码
});
//4、gcd只执行一次
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//只执行一次代码
});
//5、有三个任务,需要异步并发执行前两个任务,前两个任务执行完成后再执行第三个任务。
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//创建组
dispatch_group_t group=dispatch_group_create();
// 关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//任务一
NSLog(@"******执行任务一******");
});
// 关联一个任务到group
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//任务二
NSLog(@"******执行任务二******");
});
// 等待组中的任务执行完毕,回到主线程执行block回调
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//任务三
NSLog(@"******等待组中的任务执行完毕,回到主线程执行block回调,执行任务三******");
});
});
//6、dispatch_barrier_async的使用,dispatch_barrier_async是在前面的任务执行结束后它才执行,而且它后面的任务等它执行完成之后才会执行
dispatch_queue_t queue = dispatch_queue_create("create_asy_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"dispatch_async1");
});
dispatch_async(queue, ^{
NSLog(@"dispatch_async2");
});
dispatch_barrier_async(queue, ^{
NSLog(@"dispatch_barrier_async");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"刷新界面");
});
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"dispatch_async3");
});
/*7、GCD的另一个用处是可以让程序在后台较长久的运行。
在没有使用GCD时,当app被按home键退出后,app仅有最多5秒钟的时候做一些保存或清理资源的工作。但是在使用GCD后,app最多有10分钟的时间在后台长久运行。这个时间可以用来做清理本地缓存,发送统计数据等工作。
让程序在后台长久运行的示例代码如下:
*/
// AppDelegate.h文件
@property (assign, nonatomic) UIBackgroundTaskIdentifier backgroundUpdateTask;
// AppDelegate.m文件
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[self beingBackgroundUpdateTask];
// 在这里加上你需要长久运行的代码
[self endBackgroundUpdateTask];
}
- (void)beingBackgroundUpdateTask
{
self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[self endBackgroundUpdateTask];
}];
}
- (void)endBackgroundUpdateTask
{
[[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}
网友评论