昨天我们用程序码介绍完四种组合,想必大家对GCD有更进一步的了解了,那假设我们想让GCD的执行绪有优先顺序的话该怎麽办呢?
答案很简单,Swift里有一个Enumeration叫做DispatchQoS.QoSClass,这里定义了执行绪的执行先後顺序
优先权由高到低如下:
userInteractive > userInitiated > default > utility > background > unspecified
除了自己定义queue,也可以使用系统定义好的global queue,预设是concurrent,可以使用qos跟sync或async的属性
我们以async的情况下来说,优先权高的执行绪会先被执行,而当优先权相同时则会轮流执行
let queue1 = DispatchQueue.global(qos: .userInteractive)
let queue2 = DispatchQueue.global(qos: .background)
let queue3 = DispatchQueue.global(qos: .userInitiated)
queue1.async {
for i in 1...5{
print("?",i)
}
}
queue2.async {
for i in 11...15{
print("?",i)
}
}
queue3.async {
for i in 101...105{
print("?",i)
}
}
执行结果
由於?的优先权高於?,而?的优先权最低,所以处理顺序是?>?>?
let queue1 = DispatchQueue.global(qos: .userInteractive)
let queue2 = DispatchQueue.global(qos: .userInteractive)
let queue3 = DispatchQueue.global(qos: .userInteractive)
queue1.async {
for i in 1...5{
print("?",i)
}
}
queue2.async {
for i in 11...15{
print("?",i)
}
}
queue3.async {
for i in 101...105{
print("?",i)
}
}
执行结果
那我们GCD就介绍到这边啦~当然GCD还有很多更深入的东西,就让各位自己去发掘啦
终於完成这次的铁人赛啦~感谢大家这30天的不离不弃,如果有缘,我们明年见!
>>: 30. CSS 的 z-index & Stacking Context 的形成
UninstallView 今天来认识这个不知道是什麽的工具,解除安装概览? UninstallVi...
我们很常会在function中看到使用this这个关键字。但它是甚麽,要怎麽用?听说它的判断方法很麻...
撰写中 在求发展的道路上,又过了一日...... 这时,成员 18 人。 ...
主要呈现实作成果 以下内容有参考教学影片,底下有附网址。 (内容包括我的不专业解说分析及在实作过程中...
PHP函数 自定义函数 函数可用以下语法来定义 <?php function foo($arg...