任务:是在线程中执行的,分为同步和异步
队列:先进先出(FIFO(First Input First Output)),分为串行、并行、全局和主队列。队列只是负责任务的调度,而不负责任务的执行
1、串行(serial)
let queue = DispatchQueue(label: "label")
同步(不会开启新线程)
queue.sync {
print("serial sync thread", Thread.current)
}
异步(会开启新线程)
queue.async {
print("serial async thread", Thread.current)
}
2、并行(concurrent)
let queue = DispatchQueue(label: "label", attributes: .concurrent)
同步(不会开启新线程)
queue.sync {
print("concurrent sync thread", Thread.current)
}
异步(会开启新线程)
queue.async {
print("concurrent async thread", Thread.current)
}
3、全局(global)
let queue = DispatchQueue.global()
同步(不会开启新线程)
queue.sync {
print("global sync thread", Thread.current)
}
异步(会开启新线程)
queue.async {
print("global async thread", Thread.current)
}
4、主队列(main)
let queue = DispatchQueue.main
同步(死锁)
queue.sync {
print("main sync thread", Thread.current)
}
异步(不会开启新线程,还是在主线程)
queue.async {
print("main async thread", Thread.current)
}
网友评论