GCD代码记录,以便后用
//自定义一个queue:
- (void)customQueue{
/*
第二个参数传:
串行队列:DISPATCH_QUEUE_SERIAL 开启一个子线程,按序执行
并行队列:DISPATCH_QUEUE_CONCURRENT 开启多个子线程,无序执行
同步:串行、并行都会在主线程
异步:串行、并行都会在子线程
*/
//串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("com.gcd.serialQueue", DISPATCH_QUEUE_SERIAL);
//并发队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("com.gcd.concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//可以通过dispatch_queue_get_label(dispatch_queue_t queue)获取你创建queue的名字
const char *c = dispatch_queue_get_label(concurrentQueue);
NSLog(@"%s",c);//com.gcd.concurrentQueue
dispatch_sync(concurrentQueue, ^{
NSLog(@"1");
[NSThread sleepForTimeInterval:2];
NSLog(@"1--");
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"2");
[NSThread sleepForTimeInterval:2];
NSLog(@"2--");
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"3");
[NSThread sleepForTimeInterval:4];
NSLog(@"3--");
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"4");
[NSThread sleepForTimeInterval:2];
NSLog(@"4--");
});
}
dispatch_group
/*
串行队列:DISPATCH_QUEUE_SERIAL 开启一个子线程,按序执行
并行队列:DISPATCH_QUEUE_CONCURRENT 开启多个子线程,无序执行
*/
- (void)dispatch_group{
dispatch_queue_t queue = dispatch_queue_create("com.gcd.serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1");
[NSThread sleepForTimeInterval:5];
NSLog(@"1--");
});
dispatch_group_async(group, queue, ^{
NSLog(@"2");
[NSThread sleepForTimeInterval:8];
NSLog(@"2--");
});
dispatch_group_async(group, queue, ^{
NSLog(@"3");
[NSThread sleepForTimeInterval:3];
NSLog(@"3--");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"notify:任务都完成了");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"回到主线程");
});
});
}
dispatch_group_enter和dispatch_group_leave是成对出现的,可以按照引用计数+1、-1理解。
/*
串行队列:DISPATCH_QUEUE_SERIAL 开启一个子线程,按序执行
并行队列:DISPATCH_QUEUE_CONCURRENT 开启多个子线程,无序执行
注意:dispatch_group_notify会与耗时最长的线程在同一个线程
*/
- (void)dispatch_group_enter{
dispatch_queue_t queue = dispatch_queue_create("com.GCD.group", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group);
dispatch_async(queue, ^{
sleep(5);
NSLog(@"任务一完成");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
sleep(8);
NSLog(@"任务二完成");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
sleep(3);
NSLog(@"任务三完成");
dispatch_group_leave(group);
});
dispatch_group_notify(group, queue, ^{
NSLog(@"任务完成");
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"回到主线程");
});
});
}
dispatch_barrier
/*
dispatch_barrier_sync和dispatch_barrier_async的共同点:
1、都会等待在它前面插入队列的任务(1、2、3)先执行完
2、都会等待他们自己的任务(0)执行完再执行后面的任务(4、5、6)
dispatch_barrier_sync和dispatch_barrier_async的不共同点:
在将任务插入到queue的时候,dispatch_barrier_sync需要等待自己的任务(0)结束之后才会继续程序,然后插入被写在它后面的任务(4、5、6),然后执行后面的任务
而dispatch_barrier_async将自己的任务(0)插入到queue之后,不会等待自己的任务结束,它会继续把后面的任务(4、5、6)插入到queue
*/
- (void)dispatch_barrier{
dispatch_queue_t queue = dispatch_queue_create("com.GCD.barrier", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"1");
[NSThread sleepForTimeInterval:3];
NSLog(@"1--");
});
dispatch_async(queue, ^{
NSLog(@"2");
[NSThread sleepForTimeInterval:5];
NSLog(@"2--");
});
dispatch_async(queue, ^{
NSLog(@"3");
[NSThread sleepForTimeInterval:2];
NSLog(@"3--");
});
dispatch_barrier_async(queue, ^{
NSLog(@"barrier");
[NSThread sleepForTimeInterval:2];
NSLog(@"barrier--");
[NSThread sleepForTimeInterval:3];
NSLog(@"barrier==");
});
NSLog(@"aaa");
dispatch_async(queue, ^{
NSLog(@"4");
[NSThread sleepForTimeInterval:3];
NSLog(@"4--");
});
NSLog(@"bbb");
dispatch_async(queue, ^{
NSLog(@"5");
[NSThread sleepForTimeInterval:5];
NSLog(@"5--");
});
dispatch_async(queue, ^{
NSLog(@"6");
[NSThread sleepForTimeInterval:2];
NSLog(@"6--");
});
}