1,两个异步执行完了,才继续执行,柱塞了主线程
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印当前线程
NSLog(@"semaphore---begin");
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
__block int number = 0;
dispatch_async(queue, ^{
// 追加任务1
NSLog(@"1---%@",[NSThread currentThread]); // 打印当前线程
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"11---%@",[NSThread currentThread]); // 打印当前线程
number = 100;
dispatch_semaphore_signal(semaphore);
});
dispatch_async(queue, ^{
// 追加任务1
NSLog(@"2---%@",[NSThread currentThread]); // 打印当前线程
[NSThread sleepForTimeInterval:1]; // 模拟耗时操作
NSLog(@"22---%@",[NSThread currentThread]); // 打印当前线程
number = 100;
dispatch_semaphore_signal(semaphore);
});
NSLog(@"3");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"4");
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"semaphore---end,number = %zd",number);
2,三个异步执行完了,才继续执行,不柱塞主线程
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, ^{ /*加载图片1 */
NSLog(@"1开始");
[NSThread sleepForTimeInterval:3];
NSLog(@"1完成%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{ /*加载图片2 */
NSLog(@"2开始");
[NSThread sleepForTimeInterval:1];
NSLog(@"2完成%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{ /*加载图片3 */
NSLog(@"3开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"3完成%@",[NSThread currentThread]);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 合并图片… …这里就可以等待了
NSLog(@"完成%@",[NSThread currentThread]);
});
NSLog(@"主线程做其他事");
-----------------------------------------------
3,三个异步执行完了,才继续执行,柱塞主线程
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, ^{ /*加载图片1 */
NSLog(@"1开始");
[NSThread sleepForTimeInterval:3];
NSLog(@"1完成%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{ /*加载图片2 */
NSLog(@"2开始");
[NSThread sleepForTimeInterval:1];
NSLog(@"2完成%@",[NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{ /*加载图片3 */
NSLog(@"3开始");
[NSThread sleepForTimeInterval:2];
NSLog(@"3完成%@",[NSThread currentThread]);
});
NSLog(@"等待啊");
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"主线程做其他事");
----------------------------------------
4,添加barrier障碍操作,会等待前面的并发操作结束,并暂时阻塞后面的并发操作直到其完成,不柱塞主线程
/* 创建并发队列 */
dispatch_queue_t concurrentQueue = dispatch_queue_create("test.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
/* 添加两个并发操作A和B,即A和B会并发执行 */
dispatch_async(concurrentQueue, ^(){
NSLog(@"OperationA%@",[NSThread currentThread]);
NSLog(@"OperationA完成%@",[NSThread currentThread]);
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"OperationB%@",[NSThread currentThread]);
[NSThread sleepForTimeInterval:2];
NSLog(@"OperationB完成%@",[NSThread currentThread]);
});
/* 添加barrier障碍操作,会等待前面的并发操作结束,并暂时阻塞后面的并发操作直到其完成 */
dispatch_barrier_async(concurrentQueue, ^(){
NSLog(@"OperationBarrier!");
});
/* 继续添加并发操作C和D,要等待barrier障碍操作结束才能开始 */
dispatch_async(concurrentQueue, ^(){
NSLog(@"OperationC");
});
dispatch_async(concurrentQueue, ^(){
NSLog(@"OperationD");
});
NSLog(@"主线程做其他事");
-------------------------------------------------------
5,使用信号量实现任务2依赖于任务1,即任务2要等待任务1结束才开始执行:
/* 创建一个信号量 */
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
/* 任务1 */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* 耗时任务1 */
NSLog(@"任务1开始");
[NSThread sleepForTimeInterval:3];
NSLog(@"任务1结束");
/* 任务1结束,发送信号告诉任务2可以开始了 */
dispatch_semaphore_signal(semaphore);
});
/* 任务2 */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* 等待任务1结束获得信号量, 无限等待 */
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
/* 如果获得信号量则开始任务2 */
NSLog(@"任务2开始");
[NSThread sleepForTimeInterval:3];
NSLog(@"任务2结束");
});
NSLog(@"主线程做其他事");
----------------------------------------------------------
6,模拟1000个等待执行的任务,通过信号量控制最大并发任务数量为5
/* 创建一个信号量并初始化为5 */
dispatch_semaphore_t semaphore = dispatch_semaphore_create(5);
/* 模拟1000个等待执行的任务,通过信号量控制最大并发任务数量为5 */
for (int i = 0; i < 1000; i++) {
/* 任务i */
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
/* 耗时任务1,执行前等待信号使信号量减1 */
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
NSLog(@"任务%d开始", i);
[NSThread sleepForTimeInterval:2];
NSLog(@"任务%d结束", i);
/* 任务i结束,发送信号释放一个资源 */
dispatch_semaphore_signal(semaphore);
});
}
NSLog(@"主线程做其他事");