I find that serial queue will use more than one thread to run async code.
Here's the test code in playground.
import Foundation
let q = DispatchQueue(label: "test")
q.async {
print("hi \(Thread.current)")
}
q.async {
print("hi \(Thread.current)")
}
q.async {
print("hi \(Thread.current)")
}
q.async {
print("hi \(Thread.current)")
}
q.async {
print("hi \(Thread.current)")
}
when I repeatedly execute the playground, there will be output like this sometimes. In my understanding, serial queue should use only one thread, but the log shows it used 2 threads. I am really confused on this.
hi <NSThread: 0x7fc26a467b90>{number = 2, name = (null)}
hi <NSThread: 0x7fc26a467b90>{number = 2, name = (null)}
hi <NSThread: 0x7fc26a467b90>{number = 2, name = (null)}
hi <NSThread: 0x7fc26a467b90>{number = 2, name = (null)}
hi <NSThread: 0x7fc26b1003e0>{number = 3, name = (null)}