grand-central-dispatch – 使用`sync`调度队列和使用带有.wait`
发布时间:2020-12-14 05:42:41 所属栏目:Java 来源:网络整理
导读:我正在学习Apple的GCD,并观看视频 Concurrent Programming With GCD in Swift 3. 在这个视频的16:00,DispatchWorkItem的一个标志被称为.wait,功能和图表都显示我认为myQueue.sync(execute :)的确切内容. 所以,我的问题是;有什么区别: myQueue.sync { sleep(
我正在学习Apple的GCD,并观看视频
Concurrent Programming With GCD in Swift 3.
在这个视频的16:00,DispatchWorkItem的一个标志被称为.wait,功能和图表都显示我认为myQueue.sync(execute :)的确切内容. 所以,我的问题是;有什么区别: myQueue.sync { sleep(1); print("sync") } 和: myQueue.async(flags: .wait) { sleep(1); print("wait") } // NOTE: This syntax doesn't compile,I'm not sure where the `.wait` flag moved to. // `.wait` Seems not to be in the DispatchWorkItemFlags enum. 似乎这两种方法在等待命名队列时阻塞当前线程: >完成任何当前或以前的工作(如果是连续的) 我对此的理解必须在某处,我错过了什么? 解决方法
.wait不是DispatchWorkItemFlags中的标志,这就是原因
你的代码 myQueue.async(flags: .wait) { sleep(1); print("wait") } 不编译.
/*! * @function dispatch_block_wait * * @abstract * Wait synchronously until execution of the specified dispatch block object has * completed or until the specified timeout has elapsed. 简单的例子: let myQueue = DispatchQueue(label: "my.queue",attributes: .concurrent) let workItem = DispatchWorkItem { sleep(1) print("done") } myQueue.async(execute: workItem) print("before waiting") workItem.wait() print("after waiting") dispatchMain() (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |