OC
/**
* 隊(duì)列組 dispatch_group_notify
*/
- (void)groupNotify {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當(dāng)前線程
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任務(wù) 1
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印當(dāng)前線程
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任務(wù) 2
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印當(dāng)前線程
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的異步任務(wù) 1阿纤、任務(wù) 2 都執(zhí)行完畢后句灌,回到主線程執(zhí)行下邊任務(wù)
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"3---%@",[NSThread currentThread]); // 打印當(dāng)前線程
NSLog(@"group---end");
});
}
/**
* 隊(duì)列組 dispatch_group_wait
*/
- (void)groupWait {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當(dāng)前線程
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任務(wù) 1
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印當(dāng)前線程
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// 追加任務(wù) 2
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印當(dāng)前線程
});
// 等待上面的任務(wù)全部完成后,會(huì)往下繼續(xù)執(zhí)行(會(huì)阻塞當(dāng)前線程)
dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
NSLog(@"group---end");
}
/**
* 隊(duì)列組 dispatch_group_enter欠拾、dispatch_group_leave
*/
- (void)groupEnterAndLeave {
NSLog(@"currentThread---%@",[NSThread currentThread]); // 打印當(dāng)前線程
NSLog(@"group---begin");
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(queue, ^{
// 追加任務(wù) 1
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"1---%@",[NSThread currentThread]); // 打印當(dāng)前線程
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
// 追加任務(wù) 2
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"2---%@",[NSThread currentThread]); // 打印當(dāng)前線程
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
// 等前面的異步操作都執(zhí)行完畢后胰锌,回到主線程.
[NSThread sleepForTimeInterval:2]; // 模擬耗時(shí)操作
NSLog(@"3---%@",[NSThread currentThread]); // 打印當(dāng)前線程
NSLog(@"group---end");
});
//從 dispatch_group_enter、dispatch_group_leave
//相關(guān)代碼運(yùn)行結(jié)果中可以看出:當(dāng)所有任務(wù)執(zhí)行完成之后藐窄,才執(zhí)行 dispatch_group_notify 中的任務(wù)格带。
//這里的dispatch_group_enter屈呕、dispatch_group_leave 組合,其實(shí)等同于dispatch_group_async嗽桩。
}
Swift
//MARK: ---隊(duì)列組---
/**
* 隊(duì)列組 dispatch_group_notify
*/
func groupNotify() {
let queue = DispatchQueue.main
let group = DispatchGroup.init()
// group( asyncSerial() )
queue.async(group: group, execute: {
//追加任務(wù)1
print("追加任務(wù)1")
})
queue.async(group: group, execute: {
//追加任務(wù)2
print("追加任務(wù)2")
})
group.notify(queue: DispatchQueue.main) {
// 等前面的異步任務(wù) 1种樱、任務(wù) 2 都執(zhí)行完畢后消恍,回到主線程執(zhí)行下邊任務(wù)
print("任務(wù)3")
}
}
/**
* 隊(duì)列組 dispatch_group_wait
*/
func groupWait() {
let group = DispatchGroup.init()
let queue = DispatchQueue.global()
queue.async(group: group, execute: {
//追加任務(wù)1
print("追加任務(wù)1")
})
queue.async(group: group, execute: {
//追加任務(wù)2
print("追加任務(wù)2")
})
group.wait(timeout: .now()+1)
}
/**
* 隊(duì)列組 dispatch_group_enter邑遏、dispatch_group_leave
*/
func groupEnterAndLeave(){
let group = DispatchGroup.init()
let queue = DispatchQueue.global()
group.enter()
queue.async(group: group, execute: {
//追加任務(wù)1
print("追加任務(wù)1")
group.leave()
})
group.enter()
queue.async(group: group, execute: {
//追加任務(wù)2
print("追加任務(wù)2")
group.leave()
})
group.notify(queue: DispatchQueue.main) {
// 等前面的異步任務(wù) 1、任務(wù) 2 都執(zhí)行完畢后,回到主線程執(zhí)行下邊任務(wù)
print("任務(wù)3")
}
}