分派的工作不会花费足够长的时间来体验并行执行.第一个可能跑得太快,以至于在第二个有机会开始之前就结束了.(print
输出是同步的,这一事实加剧了这一点.)try 插入Thread.sleep(…)
(您在生产代码中永远不会这样做,但对于诊断目的很有用)或一些耗时的事情.
例如:
func myConcurrentQueue() {
let concurrentQueue = DispatchQueue(label: "concurrent", attributes: .concurrent)
let start = ContinuousClock().now
concurrentQueue.async {
print("Task 1 Con Start at", start.duration(to: .now))
Thread.sleep(forTimeInterval: 1) // never do this in production app; just for diagnostic purposes
print("Task 1 Con Finished at", start.duration(to: .now))
}
concurrentQueue.async {
print("Task 2 Con Start at", start.duration(to: .now))
Thread.sleep(forTimeInterval: 1) // never do this in production app; just for diagnostic purposes
print("Task 2 Con Finished at", start.duration(to: .now))
}
}
我可能建议包含一些信息,以显示有关何时生成输出的信息(因此我在上面使用了ContinuousClock
).
无论如何,这会产生类似的结果:
Task 1 Con Start at 0.001328375 seconds
Task 2 Con Start at 0.001392083 seconds
Task 2 Con Finished at 1.006529791 seconds
Task 1 Con Finished at 1.006475916 seconds
注意,因为它们是并行运行的,所以从技术上讲,您无法确定哪一项最先到达其各自的print
条语句(因此,在本例中,任务2恰好比任务1早完成零点几毫秒),但您可以看到它们并行运行.
而且,在回答你的问题时,不,标签的 Select 并不重要.仅用于诊断目的.
另外,请注意,仅仅因为您使用并发队列并不能保证您一定会经历并行执行.这取决于其他核心是否在忙于做其他事情.此外,我对在操场上测试这类东西持谨慎态度(如果你是这样的话),因为这有时会表现出与真实应用程序相当不典型的执行特征.
可视化并行执行的另一种方法是使用工具中的"兴趣点"工具,如How to identify key events in Xcode Instruments?中所述
import os.signpost
let poi = OSSignposter(subsystem: "Test", category: .pointsOfInterest)
func myConcurrentQueue() {
let concurrentQueue = DispatchQueue(label: "concurrent", attributes: .concurrent)
concurrentQueue.async {
poi.withIntervalSignpost("1") {
Thread.sleep(forTimeInterval: 1) // never do this in production app; just for diagnostic purposes
}
}
concurrentQueue.async {
poi.withIntervalSignpost("2") {
Thread.sleep(forTimeInterval: 1)
}
}
}
这使我们可以从Instruments中可视化并发队列在时间线上的行为,并且您可以看到它们并行运行.但同样,我们之所以看到这一点,是因为我让它做了一些足够慢的操作,使得第一个工作项很可能在第二个工作项有机会启动之前无法完成:
请注意,现在我们通常不会使用GCD并发队列.我们将改用SWIFT并发性,在本例中为"task group":
func myTaskGroup() async {
await withTaskGroup(of: Void.self) { group in
group.addTask { // first task
…
}
group.addTask { // second task
…
}
}
}
有关更多信息,请参阅WWDC 2021视频12:5232/" rel="nofollow noreferrer">Meet async/await in Swift.并在Explore structured concurrency in Swift中的12:52处引入任务组.
归根结底,SWIFT并发性超出了问题的范围,但我想分享并发调度队列的现代替代方案.