DispatchGroup.wait()和DispatchGro
作者:
狗蛋的春天 | 来源:发表于
2020-09-16 21:32 被阅读0次
wait()会等待线程执行完载执行之后的逻辑,阻塞当前线程
let myQuenue = DispatchQueue(label: "myQuneue")
let myGroup = DispatchGroup()
myGroup.enter()
myQuenue.async {
Thread.sleep(forTimeInterval: 5)
print("哈哈哈哈哈哈哈哈哈哈")
myGroup.leave()
}
myGroup.enter()
myQuenue.async {
Thread.sleep(forTimeInterval: 1)
print("kkkkkkkk")
myGroup.leave()
}
myGroup.wait()
print("咔咔咔咔咔咔扩扩扩扩扩")
结果:哈哈哈哈哈哈哈哈哈哈
kkkkkkkk
咔咔咔咔咔咔扩扩扩扩扩
notify()不会阻塞当前线程,先执行notify后面的同步方法
let myQuenue = DispatchQueue(label: "myQuneue")
let myGroup = DispatchGroup()
myGroup.enter()
myQuenue.async {
Thread.sleep(forTimeInterval: 5)
print("哈哈哈哈哈哈哈哈哈哈")
myGroup.leave()
}
myGroup.enter()
myQuenue.async {
Thread.sleep(forTimeInterval: 1)
print("kkkkkkkk")
myGroup.leave()
}
myGroup.notify(queue: myQuenue) {
print("k啦啦啦啦啦啦啦啦")
}
print("呜呜呜呜呜呜呜呜无无")
结果:呜呜呜呜呜呜呜呜无无
哈哈哈哈哈哈哈哈哈哈
kkkkkkkk
咔咔咔咔咔咔扩扩扩扩扩
本文标题:DispatchGroup.wait()和DispatchGro
本文链接:https://www.haomeiwen.com/subject/ogahyktx.html
网友评论