多線程應(yīng)用
GCD先執(zhí)行ABC,都完成后再執(zhí)行D
+(void)test1{
//創(chuàng)建隊(duì)列組
dispatch_group_t group = dispatch_group_create();
//進(jìn)入這個(gè)組
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-");
//這個(gè)組的任務(wù)完成時(shí)離開
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);
});
});
//當(dāng)所有的任務(wù)都完成后會(huì)發(fā)送這個(gè)通知
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"ABC 任務(wù)全部完成 ,開始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 任務(wù)全部完成
NSOperation 先執(zhí)行AB在執(zhí)行C
+ (void)addDependency {
// 1.創(chuàng)建隊(duì)列
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
// 2.創(chuàng)建操作
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"1---%@", [NSThread currentThread]); // 打印當(dāng)前線程
}
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"2---%@", [NSThread currentThread]); // 打印當(dāng)前線程
}
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
for (int i = 0; i < 2; i++) {
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"3---%@", [NSThread currentThread]); // 打印當(dāng)前線程
}
}];
// 3.添加依賴
[op3 addDependency:op1]; // 讓op2 依賴于 op1,則先執(zhí)行op1萄涯,在執(zhí)行op2
[op3 addDependency:op2];
// // 4.添加操作到隊(duì)列中
// [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)}
覺得有用點(diǎn)個(gè)喜歡,謝謝!