GCD的使用在Swift3中的方法
//串行队列
let q:DispatchQueue = DispatchQueue(label: "xiaosi")
//并发队列 qos : 为 优先级别 可以不设置
let queue = DispatchQueue(label: "com.appcoda.anotherQueue", qos: .userInitiated, attributes: .concurrent)
使用最多的是异步并发
queue.async {
print("异步并发11111(Thread.current)")
}
queue.async {
print("异步并发22222(Thread.current)")
}
queue.async {
print("异步并发33333(Thread.current)")
}
queue.async {
print("异步并发44444(Thread.current)")
}
其次是异步串行
queue.async {
print("异步串行11111(Thread.current)")
}
queue.async {
print("异步串行2222(Thread.current)")
}
queue.async {
print("异步串行4444(Thread.current)")
}
queue.async {
print("异步串行5555(Thread.current)")
}
还有就是线程组
let group = DispatchGroup()
q.async(group: group) {
sleep(2)
print("异步任务3333(Thread.current)")
}
q.async(group: group) {
sleep(2)
print("异步任务4444(Thread.current)")
}
group.notify(queue: q) {
print("全部做完了")
}