我认为DISPATCH_QUEUE_CONCURRENT
随机工作,但它似乎按顺序工作。
我的代码有问题吗?
码
let concurrentDispatchQueue = dispatch_queue_create("serialDispatchQueue", DISPATCH_QUEUE_CONCURRENT)
func foo(name: String, queue: dispatch_queue_t) {
for i in 1...10 {
dispatch_sync(queue) { () -> Void in
let r = Double(random() % 10)
NSThread.sleepForTimeInterval(0.02 * r)
print("\(name) \(i)")
}
}
}
foo("concurrent", queue: concurrentDispatchQueue)
输出
concurrent 1 concurrent 2 concurrent 3 concurrent 4 concurrent 5 concurrent 6 concurrent 7 concurrent 8 concurrent 9 concurrent 10
答案 0 :(得分:2)
您正在将同步分派给队列。 每个
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
在执行闭包之后仅返回。去测试 队列上的并发执行,将调用更改为
{{1}}
请注意,您必须添加
{{1}}
如果您在Playground中进行测试,请参阅 How do I run Asynchronous callbacks in Playground