Swift GCD

参考链接

主线程
DispatchQueue.main.async {
    NSLog("\(Thread.current)")
}
串行队列
  • 同步依次执行,在主线程依次执行
let queue = DispatchQueue(label: "test")
for i in 0..<10 {
    queue.sync {
        NSLog("\(Thread.current) -- \(i)")
     }
}
  • 同步依次执行,在同一个线程执行
let queue = DispatchQueue(label: "test")
for i in 0..<10 {
    queue.async {
        NSLog("\(Thread.current) -- \(i)")
     }
}
并行队列
  • 创建多个线程,执行顺序不确定
let queue = DispatchQueue(label: "test", qos: .default, attributes: .concurrent, autoreleaseFrequency: .inherit, target: nil)
for i in 0..<10 {
    queue.async {
        NSLog("\(Thread.current) -- \(i)")
    }
}

你可能感兴趣的:(Swift GCD)