gpt4 book ai didi

ios - iOS GCD全局队列优先级均为0.5

转载 作者:行者123 更新时间:2023-12-01 16:08:41 25 4
gpt4 key购买 nike

我遇到的问题是为什么以下代码打印出来的优先级是相同的?为什么是这样?非常感谢你。

    print("main:\(Thread.current)")
print("thread main priorities:\(Thread.current.threadPriority)")

DispatchQueue.global(qos: .utility).async {
print("utility:\(Thread.current)")
print("thread utility priorities:\(Thread.current.threadPriority)")
}

DispatchQueue.global(qos: .background).async {
print("background:\(Thread.current)")
print("thread background priorities:\(Thread.current.threadPriority)")
}

DispatchQueue.global(qos: .userInteractive).async {
print("userInteractive:\(Thread.current)")
print("thread userInteractive priorities:\(Thread.current.threadPriority)")
}

DispatchQueue.global(qos: .userInitiated).async {
print("userInitiated:\(Thread.current)")
print("thread userInitiated priorities:\(Thread.current.threadPriority)")
}

日志:

主要:{number = 1,name = main}
线程主要优先级:0.5

userInteractive:{数字= 3,名称=(空)}

实用程序:{数字= 5,名称=(空)}

背景:{数字= 6,名称=(空)}

线程用户交互优先级:0.5

userInitiated:{数字= 4,名称=(空)}

线程实用程序优先级:0.5

线程背景优先级:0.5

线程用户启动的优先级:0.5

最佳答案

正如您在文档中看到的:

@available(iOS 4.0, *)
open var threadPriority: Double // To be deprecated; use qualityOfService below

不推荐使用threadPriority。此外,Apple的GCD团队在“线程-队列”主题上也很清楚。 GCD将为您管理线程。队列优先级不是线程优先级。看一下这个示例,几乎就是您在此处发布的代码,我刚刚添加了“qos_class_self()。rawValue”,这实际上是您应该如何测试QoS的方法,而不是“threadPriority”。
print("main:\(Thread.current), Thread main priorities:\(Thread.current.threadPriority), QoS: \(qos_class_self().rawValue)")

DispatchQueue.global(qos: .utility).async {
print("utility:\(Thread.current), Thread utility priorities:\(Thread.current.threadPriority), QoS: \(qos_class_self().rawValue)")
}

DispatchQueue.global(qos: .background).async {
print("background:\(Thread.current), Thread background priorities:\(Thread.current.threadPriority), QoS: \(qos_class_self().rawValue)")
}

DispatchQueue.global(qos: .userInteractive).async {
print("userInteractive:\(Thread.current), Thread userInteractive priorities:\(Thread.current.threadPriority), QoS: \(qos_class_self().rawValue)")
}

DispatchQueue.global(qos: .userInitiated).async {
print("userInitiated:\(Thread.current), Thread userInitiated priorities:\(Thread.current.threadPriority), QoS: \(qos_class_self().rawValue)")
}

我还将每个封口的打印合并在一起,以免它们在日志的不同行中混乱。这表明您维护了QoS,但不赞成使用threadPriority。
main:<NSThread: 0x6100000762c0>{number = 1, name = main}, Thread main priorities:0.5, QoS: 33
userInteractive:<NSThread: 0x610000261fc0>{number = 4, name = (null)}, Thread userInteractive priorities:0.5, QoS: 33
utility:<NSThread: 0x618000079100>{number = 3, name = (null)}, Thread utility priorities:0.5, QoS: 17
userInitiated:<NSThread: 0x608000078380>{number = 5, name = (null)}, Thread userInitiated priorities:0.5, QoS: 25
background:<NSThread: 0x610000262000>{number = 6, name = (null)}, Thread background priorities:0.5, QoS: 9

因此,最终,解决的方法不是考虑线程,而是考虑队列,QoS和知道GCD将为您管理线程。与Queues与GCD出现之前的习惯截然不同。

关于ios - iOS GCD全局队列优先级均为0.5,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44358144/

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