方式一
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
开启异步任务
successBlock{
dispatch_group_leave(group);
}
failedBlock{
dispatch_group_leave(group);
}
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
异步任务完成后 需要做的事情....
});
方式二
self.lock = [[NSConditionLock alloc]initWithCondition:1];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lockWhenCondition:1];
for (int i = 0; i < 10; i ++) {
NSLog(@"---%d---",i);
}
[self.lock unlockWithCondition:2];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lockWhenCondition:2];
for (int i = 0; i < 10; i ++) {
NSLog(@"+++%d+++",i);
}
[self.lock unlockWithCondition:3];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self.lock lockWhenCondition:3];
for (int i = 0; i < 10; i ++) {
NSLog(@"***%d***",i);
}
[self.lock lock];
});
方式三
dispatch_queue_t queue = dispatch_queue_create("queue", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
sleep(3);
NSLog(@"111111111111111111");
});
dispatch_async(queue, ^{
sleep(2);
NSLog(@"2222222222222222222");
});
dispatch_async(queue, ^{
sleep(1);
NSLog(@"333333333333333333");
});
GCD信号量详解
- (void)viewDidLoad {
[super viewDidLoad];
//表示最多5个异步任务会进来访问
self.semaphore = dispatch_semaphore_create(5);
for (int i = 0; i < 20; i ++) {
[[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil] start];
}
}
- (void)test
{
//dispatch_semaphore_wait 判断信号量的值 如果信号量的值 > 0 就让信号量的值减1 然后继续往下执行
//如果信号量的值<= 0就会休眠等待 直到信号量的值>0 然后让信号量的值减1 然后继续往下执行
//DISPATCH_TIME_FOREVER表示一直等
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
sleep(2);
NSLog(@"%@",[NSThread currentThread]);
//让信号量的值+1
dispatch_semaphore_signal(self.semaphore);
}
self.semaphore = dispatch_semaphore_create(1);
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"--------111---------");
}
dispatch_semaphore_signal(self.semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"++++++++222+++++++++");
}
dispatch_semaphore_signal(self.semaphore);
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
dispatch_semaphore_wait(self.semaphore, DISPATCH_TIME_FOREVER);
for (int i = 0; i < 10; i ++) {
NSLog(@"*********333***********");
}
dispatch_semaphore_signal(self.semaphore);
});
方式四
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"------1111------");
}
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"------2222------");
}
}
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
@synchronized (self) {
for (int i = 0; i < 10; i ++) {
NSLog(@"*****3333******");
}
}
});
栅栏函数
//dispatch_barrier_async 必须传入自己创建的并发队列 不能是全局并发队列 也不能是串行队列
dispatch_queue_t queue = dispatch_queue_create("rw_queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"多读");
});
dispatch_barrier_async(queue, ^{
NSLog(@"单写");
});