Grand Central Dispatch
;总结:将任务添加到队列,并且指定执行任务的函数。
dispatch_sync(queue , {})
获取;必须等待当前语句执行完毕
,才会执行下一条语句;不会开启其他线程
,就在当前线程中完成任务;dispatch_async(queue , {})
获取;不用等待当前语句执行完毕
,就可以执行下一条语句会开启线程
,异步就是多线程的代名词;dispatch_get_main_queue()
获取;主线程
上调度任务的串行队列;dispatch_get_global_queue(0, 0)
全局队列是并发队列
,包含四个优先级;dispatch_queue_create("Lable", NULL);
获取串行队列
、并发队列
;dispatch_sync(dispatch_get_main_queue();, ^{ });
dispatch_async(dispatch_get_main_queue();, ^{ });
dispatch_get_main_queue();
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
//全局并发队列的优先级
#define DISPATCH_QUEUE_PRIORITY_HIGH 2 // 高优先级
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // 默认(中)优先级
#define DISPATCH_QUEUE_PRIORITY_LOW (-2) // 低优先级
#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN // 后台优先级
// 串行队列
dispatch_queue_create("HRTest", DISPATCH_QUEUE_SERIAL);
// 并发队列
dispatch_queue_create("HRTest", DISPATCH_QUEUE_CONCURRENT);
-(void)demo{
dispatch_queue_t queue = dispatch_queue_create("HRTest", NULL);
dispatch_sync(queue, ^{
NSLog(@"2");
});
}
不会开启新的线程
,在当前主线程
下进行任务消耗;死锁情况
-(void)demo{
dispatch_queue_t queue = dispatch_queue_create("HRTest", NULL);
dispatch_sync(queue, ^{
dispatch_sync(queue, ^{
NSLog(@"1");
});
NSLog(@"2");
});
}
-(void)demo{
NSLog(@"%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("HRTest", NULL);
for (int i = 0; i<3; i++) {
dispatch_async(queue, ^{
NSLog(@"1- %@",[NSThread currentThread]);
});
}
for (int i = 0; i<3; i++) {
dispatch_async(queue, ^{
NSLog(@"2- %@",[NSThread currentThread]);
});
}
for (int i = 0; i<3; i++) {
dispatch_async(queue, ^{
NSLog(@"3- %@",[NSThread currentThread]);
});
}
}
输出:
只会开启一条新的线程
;死锁情况
-(void)demo{
NSLog(@"%@",[NSThread currentThread]);
dispatch_queue_t queue = dispatch_queue_create("HRTest", NULL);
dispatch_async(queue, ^{
NSLog(@"2");
dispatch_sync(queue, ^{
NSLog(@"3");
});
});
}
总体来说涉及到串行队列的嵌套就容易出现死锁,使用时一定要注意;串行队列里添加同步任务队列必定会出现死锁
;
-(void)demo{
dispatch_queue_t queue = dispatch_queue_create("cooci", DISPATCH_QUEUE_CONCURRENT);
// 耗时
dispatch_sync(queue, ^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"2");
dispatch_sync(queue, ^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"3");
});
NSLog(@"4");
});
}
输出:
不会开启新的线程
,在当前主线程
下进行任务消耗;-(void)demo{
dispatch_queue_t queue = dispatch_queue_create("cooci", DISPATCH_QUEUE_CONCURRENT);
// 耗时
dispatch_async(queue, ^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"1");
dispatch_async(queue, ^{
NSLog(@"%@",[NSThread currentThread]);
NSLog(@"2");
});
NSLog(@"3");
});
}
输出:
会产生多条线程
;// 异步
dispatch_async(dispatch_get_global_queue(0, 0), ^{
// 耗时操作放在这里
[NSThread sleepForTimeInterval:2];
// 回到主线程处理UI
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
});
-(void)demo{
NSLog(@"%@",[NSDate date]);
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",[NSDate date]);
});
}
输出:
dispatch_queue_t queue = dispatch_queue_create("HRTest", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue1, ^{
NSLog(@"1");
});
dispatch_async(queue1, ^{
NSLog(@"2");
});
dispatch_barrier_async(queue1, ^{
NSLog(@"3");
});
dispatch_async(queue1, ^{
NSLog(@"4");
});
队列组有下面几个特点:
-(void)demo{
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
//创建一个队列组
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"下载1开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"下载1结束");
});
dispatch_group_async(group, queue, ^{
NSLog(@"下载2开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"下载2结束");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"下载全部结束");
//在主线程显示
dispatch_async(dispatch_get_main_queue(), ^{
self.imageView.image = image;
});
});
}
// 创建一个信号,value:信号量
dispatch_semaphore_create(<#long value#>)
// 使某个信号的信号量+1
dispatch_semaphore_signal(<#dispatch_semaphore_t dsema#>)
// 某个信号进行等待或等待降低信号量 timeout:等待时间,永远等待为 DISPATCH_TIME_FOREVER
dispatch_semaphore_wait(<#dispatch_semaphore_t dsema#>, <#dispatch_time_t timeout#>)
先降低然后再提高
,这两个函数通常成对使用`。信号量代表可以进入的线程数
,信号量为0表示当前线程堵塞
; static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[self alloc] init];
});