///获取全局主串行队列
let OverallMainQueue = DispatchQueue.main
//全局主串行队列异步执行
OverallMainQueue.async {
for _ in 1...100{
print("OverallMainQueue" + "\(Thread.current)")
}
}
//全局主串行队列同步步执行 会导致主队列死锁 不使用
// OverallMainQueue.sync {
// for _ in 1...100{
// print("OverallMainQueue" + "\(Thread.current)")
// }
// }
///获取全局并发队列
let OverallConQueue = DispatchQueue.global()
//全局并发队列异步执行
OverallConQueue.async {
for _ in 1...100{
print("OverallConQueue" + "\(Thread.current)")
}
}
//全局并发队列异步执行
OverallConQueue.sync {
for _ in 1...100{
print("OverallConQueue" + "\(Thread.current)")
}
}
/// 创建串行队列
let serQueue = DispatchQueue(label: "ser.jiaxuli.com")
//串行队列异步执行
serQueue.async {
for _ in 1...100{
print("serQueue" + "\(Thread.current)")
}
//返回主线程
DispatchQueue.main.async {
print("currentQueue" + "\(Thread.current)")
}
}
//串行队列同步直接
serQueue.sync {
for _ in 1...100{
print("serQueue" + "\(Thread.current)")
}
}
/// 创建并行队列
let conQueue = DispatchQueue(label: "con.jiaxuli.com", qos: .default, attributes: .concurrent, autoreleaseFrequency:.inherit, target: nil)
//并行队列异步执行
conQueue.async {
for _ in 1...100{
print("conQueue" + "\(Thread.current)")
}
}
//并行队列同步执行
conQueue.sync {
for _ in 1...100{
print("conQueue" + "\(Thread.current)")
}
}
demo
网友评论