gpt4 book ai didi

ios - 多次调用DispatchQueue.global(qos : . background).async会创建多线程吗?

转载 作者:行者123 更新时间:2023-12-01 23:19:17 27 4
gpt4 key购买 nike

我对使用 Swift for iOS 进行编程还比较陌生。我想知道我是否这样做:

for n in 1...10 {
DispatchQueue.global(qos: .background).async {
sleep(5)
print("Hello world!")
}
}

这会产生 10 个后台线程,它们会立即开始处理我的任务吗?

或者是否只有 1 后台线程,而我将刚入列 10 待办事项?

最佳答案

来自DispatchQueue docs :

Work submitted to dispatch queues executes on a pool of threads managed by the system. Except for the dispatch queue representing your app's main thread, the system makes no guarantees about which thread it uses to execute a task.

When a task scheduled by a concurrent dispatch queue blocks a thread, the system creates additional threads to run other queued concurrent tasks.

系统可以使用现有线程之一,也可以创建一个新线程。不能保证在同一个调度队列上执行的两个工作项将在同一个线程上执行(除非它是主队列)。

关于ios - 多次调用DispatchQueue.global(qos : . background).async会创建多线程吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68308200/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com