OC 线程操作 - GCD队列组

1.队列组两种使用方法
2.队列组等待 wait

/** 新方法
队列组一般用在在异步操作,在主线程写队列组毫无任何作用
*/
- (void)GCD_Group_new_group___notify{
dispatch_queue_t queue = dispatch_queue_create("11", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t globalqueue = dispatch_get_global_queue(0, 0);
dispatch_group_t group = dispatch_group_create();
/*
1.封装任务
2.把任务加到队列
3.会监听任务的执行情况
*/
dispatch_group_async(group, globalqueue, ^{
NSLog(@"1111---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"22222---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"333---%@---", [NSThread currentThread]);
}); // 一定要加上这行,不然不起任何作用
dispatch_group_notify(group, globalqueue, ^{
NSLog(@"完成----4444---%@---", [NSThread currentThread]);
}); /*
打印结果:
2018-06-28 10:18:44.191407+0800 5线程操作-GCD-快速迭代[7808:56262] 333---<NSThread: 0x6000002682c0>{number = 4, name = (null)}---
2018-06-28 10:18:44.191409+0800 5线程操作-GCD-快速迭代[7808:56266] 1111---<NSThread: 0x608000075300>{number = 3, name = (null)}---
2018-06-28 10:18:44.191434+0800 5线程操作-GCD-快速迭代[7808:56264] 22222---<NSThread: 0x600000266580>{number = 5, name = (null)}---
2018-06-28 10:18:44.191758+0800 5线程操作-GCD-快速迭代[7808:56264] 完成----4444---<NSThread: 0x600000266580>{number = 5, name = (null)}---
*/
    //DISPATCH_TIME_FOREVER :等待队列所有任务执行完后 执行下面代码后面的内容
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"执行完了");
/*
打印结果:
2018-06-28 10:58:34.631990+0800 5线程操作-GCD-快速迭代[8500:86081] 1111---<NSThread: 0x60400007e180>{number = 3, name = (null)}---
2018-06-28 10:58:34.632041+0800 5线程操作-GCD-快速迭代[8500:86084] 22222---<NSThread: 0x60400007e4c0>{number = 4, name = (null)}---
2018-06-28 10:58:34.632065+0800 5线程操作-GCD-快速迭代[8500:86080] 333---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
2018-06-28 10:58:34.633261+0800 5线程操作-GCD-快速迭代[8500:86052] 执行完了
2018-06-28 10:58:34.633284+0800 5线程操作-GCD-快速迭代[8500:86080] 完成----4444---<NSThread: 0x60400007e380>{number = 5, name = (null)}---
*/
}
//老式写法
- (void)GCD_GroupDemo_old___enter_leave{
dispatch_queue_t queue = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t globalqueue = dispatch_get_global_queue(, );
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group); // 旧 写法
dispatch_group_async(group, globalqueue, ^{
NSLog(@"离开---%@---", [NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_async(group, globalqueue, ^{
NSLog(@"222---%@---", [NSThread currentThread]);
});
dispatch_group_async(group, globalqueue, ^{
NSLog(@"333---%@---", [NSThread currentThread]);
}); dispatch_group_async(group, globalqueue, ^{
NSLog(@"111---%@---", [NSThread currentThread]);
});
/*
2018-06-28 10:25:06.997243+0800 5线程操作-GCD-快速迭代[7942:62493] 离开---<NSThread: 0x60c00007f8c0>{number = 3, name = (null)}---
2018-06-28 10:25:06.997286+0800 5线程操作-GCD-快速迭代[7942:62491] 333---<NSThread: 0x60800007d240>{number = 5, name = (null)}---
2018-06-28 10:25:06.997306+0800 5线程操作-GCD-快速迭代[7942:62489] 111---<NSThread: 0x608000260c80>{number = 6, name = (null)}---
2018-06-28 10:25:06.997287+0800 5线程操作-GCD-快速迭代[7942:62492] 222---<NSThread: 0x60c00007de40>{number = 4, name = (null)}---
*/
}

上一篇:工作流Activiti入门教程


下一篇:IntersectionObserver实现图片懒加载