swift GCD 基本操作
原创
©著作权归作者所有:来自51CTO博客作者mp624183768的原创作品,请联系作者获取转载授权,否则将追究法律责任
GCD-串行 vs 并行
GCD-同步 vs 异步
GCD- sync
提交任务到当前队列里,并且直到任务完成,当前队列才会返回
GCD-async
调度一个任务区立即执行
但是不用等任务完成就会直接返回
code
//: FROM https://www.anuomob.com
import UIKit
import PlaygroundSupport
//不希望主线程执行完毕就结束
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "myQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
queue.sync {
sleep(1)
print("in queue sync")
}
print("after invo queue method")
queue.async {
sleep(1)
print("in queue async")
}
print("after invo queue method2")
in queue sync
after invo queue method
after invo queue method2
in queue async
//: FROM https://www.anuomob.com
import UIKit
import PlaygroundSupport
//不希望主线程执行完毕就结束
PlaygroundPage.current.needsIndefiniteExecution = true
let queue = DispatchQueue(label: "myQueue", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.concurrent, autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)
queue.asyncAfter(deadline: .now()+1, execute: {
print("in asyncAfter")
})
print("afger invok queue method ")
afger invok queue method
in asyncAfter