多线程应用
GCD先执行ABC,都完成后再执行D
+(void)test1{
//创建队列组
dispatch_group_t group = dispatch_group_create();
//进入这个组
dispatch_group_enter(group);
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"operationA---");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"operationA--OK-");
//这个组的任务完成时离开
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"operationB---");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"operationB--OK-");
dispatch_group_leave(group);
});
});
dispatch_group_enter(group);
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
NSLog(@"operationC---");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"operationC--OK-");
dispatch_group_leave(group);
});
});
//当所有的任务都完成后会发送这个通知
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"ABC 任务全部完成 ,开始D");
});
}
打印:
2019-04-03 15:01:26.213596+0800 GCD[598:92227] operationB---
2019-04-03 15:01:26.214236+0800 GCD[598:92227] operationC---
2019-04-03 15:01:26.214450+0800 GCD[598:92225] operationA---
2019-04-03 15:01:28.368611+0800 GCD[598:92202] operationB--OK-
2019-04-03 15:01:29.488141+0800 GCD[598:92202] operationC--OK-
2019-04-03 15:01:30.572944+0800 GCD[598:92202] operationA--OK-
2019-04-03 15:01:30.573321+0800 GCD[598:92202] ABC 任务全部完成
NSOperation 先执行AB在执行C
+ (void)addDependency {
// 1.创建队列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 2.创建操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"1---%@", [NSThread currentThread]); // 打印当前线程
}
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"2---%@", [NSThread currentThread]); // 打印当前线程
}
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模拟耗时操作
NSLog(@"3---%@", [NSThread currentThread]); // 打印当前线程
}
}];
// 3.添加依赖
[op3 addDependency:op1]; // 让op2 依赖于 op1,则先执行op1,在执行op2
[op3 addDependency:op2];
// // 4.添加操作到队列中
// [queue addOperation:op1];
// [queue addOperation:op2];
[queue addOperations:@[op1 ,op2,op3] waitUntilFinished:YES];
}
打印:
2019-04-03 15:52:04.992320+0800 NSOperation[629:96844] 1---<NSThread: 0x15d48520>{number = 4, name = (null)}
2019-04-03 15:52:04.992329+0800 NSOperation[629:96845] 2---<NSThread: 0x15e42be0>{number = 3, name = (null)}
2019-04-03 15:52:06.993761+0800 NSOperation[629:96844] 1---<NSThread: 0x15d48520>{number = 4, name = (null)}
2019-04-03 15:52:06.993772+0800 NSOperation[629:96845] 2---<NSThread: 0x15e42be0>{number = 3, name = (null)}
2019-04-03 15:52:08.995584+0800 NSOperation[629:96844] 3---<NSThread: 0x15d48520>{number = 4, name = (null)}
2019-04-03 15:52:10.996866+0800 NSOperation[629:96844] 3---<NSThread: 0x15d48520>{number = 4, name = (null)}
觉得有用点个喜欢,谢谢!