This is my first question here, so please be nice.
I struggle to understand the dynamics around a queue's target in Swift:
-
我读到过,自定义队列继承其目标队列的行为.
-
在下面的示例中,
concurrentQueue
的属性设置为.concurrent
. -
但是,因为它的目标队列是
DispatchQueue.main
,从定义上讲是连续的,所以concurrentQueue
是按顺序执行的:let concurrentQueue = DispatchQueue(label: "concurrentQueue", attributes: .concurrent, target: DispatchQueue.main) concurrentQueue.async { for i in 1...5 { print(i) } } concurrentQueue.async { for i in 6...10 { print(i) } }
输出:
1 2 3 4 5 6 7 8 9 10
-
相反,如果我有一个以全局队列为目标的定制序列队列,根据定义,它是并发的,我希望我的定制序列队列被并发执行.
-
However, my custom serial queue is still getting executed serially. Why is that?
PROBLEM STATEMENT:个
-
在这里,
serialQueue
没有定义属性,这使它成为一个序列队列. -
因为它有一个并发队列
DispatchQueue.global(qos: .background)
作为目标,所以我预计它会被并发执行. -
然而,输出仍然是连续的.
let serialQueue = DispatchQueue(label: "serialQueue", target: DispatchQueue.global(qos: .background)) serialQueue.async { for i in 1...5 { print(i) } } serialQueue.async { for i in 6...10 { print(i) } }
输出:
1 2 3 4 5 6 7 8 9 10