我正在尝试并行运行两个线程。但不幸的是,它有时工作有时不工作。这是我的代码
let firstQueue = DispatchQueue(label: "queue1", qos: DispatchQoS.userInitiated)
let secondQueue = DispatchQueue(label: "queue2", qos: DispatchQoS.userInitiated)
//let firstQueue = DispatchQueue(label: "queue1", qos: DispatchQoS.default , attributes: .concurrent)
firstQueue.async {
for i in 0..<10 {
print("", i)
}
}
secondQueue.async {
for i in 20..<30 {
print("⚪️", i)
}
}
我尝试了不同的一切,但没有达到完美的并行性。第一次输出:0
⚪️ 20
1
⚪️ 21
2
⚪️ 22
3
⚪️ 23
4
⚪️ 24
5
⚪️ 25
6
⚪️ 26
7
⚪️ 27
8
⚪️ 28
9
⚪️ 29
第二次输出:
0 1 2 3 4 5 6 7 8 9 ⚪️ 20 ⚪️ 21 ⚪️ 22 ⚪️ 23 ⚪️ 24 ⚪️ 25 ⚪️ 26 ⚪️ 27 ⚪️ 28 ⚪️ 29