一、GCD常用的队列和函数
串行队列: 让任务一个接着一个执行(一个任务)
let serialQue = DispatchQueue(label: “”)
并发队列: 可以让多个任务并发同时执行(自动开启多个线程),但需注意的是并发功能只有在.async函数才有效。
let concurrenceQueue = DispatchQueue(label: "", qos: .default, attributes: DispatchQueue.Attributes.concurrent)
全局并行队列 :
let globalQueue = DispatchQueue.global()
主队列:
let mainQueUe = DispatchQueue.main
异步的方式执行任务
serialQue.async {}
同步的方式执行任务
serialQue.sync { }
使用sync函数往当前串行队列中添加任务,会卡住当前的串行队列(产生死锁)常见下面2个例子。
例子1:
let serialQue = DispatchQueue(label: “”)
serialQue.sync {
serialQue.sync{
print("同步任务”)
}
}
例子2:
import UIKit
class ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
DispatchQueue.main.sync {
print("同步任务")
}
}
}
二、DispatchWorkItem
DispatchWorkItem 到底是个什么呢?通俗的来说,DispatchWorkItem 就是 GCD 里面常说的一段待执行的任务,更直白一点,它本质只是一个等待执行的代码块而已,可以在任意一个队列上被调用。
let workItem = DispatchWorkItem {
print("执行任务一")
}
DispatchQueue.global().async(execute: workItem)
workItem.notify(queue: DispatchQueue.main) {
print("任务执行完回到主队列刷新UI")
}
workItem.cancel()
-
workItem
的闭包里面是封装的任务 - notify是执行完任务在某个队列做一些事情,这里直接回归到主队列。
- cancel取消任务
三、延迟执行
let workItem = DispatchWorkItem {
print("延迟10s执行任务")
}
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now()+10, execute: workItem)
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now()+3) {
print("延迟3s执行任务")
}
四、栅栏函数
let concurrenceQueue = DispatchQueue(label: "", qos: .default, attributes: DispatchQueue.Attributes.concurrent)
concurrenceQueue.async {
sleep(10)
print("任务一")
}
concurrenceQueue.async {
sleep(20)
print("任务二")
}
concurrenceQueue.async(flags: .barrier) {
print("栅栏任务结束")
}
concurrenceQueue.async {
print("任务三")
}
这个例子就是任务一、任务二、任务三的任务中添加了栅栏,会导致任务三被隔离,任务、任务二任务完成之后才会去执行任务三
五、队列组
let concurrenceQueue = DispatchQueue(label: "", qos: .default, attributes: DispatchQueue.Attributes.concurrent)
let group = DispatchGroup()
group.enter()
group.enter()
concurrenceQueue.async(group: group) {
print("任务一")
group.leave()
};
concurrenceQueue.async(group: group) {
print("任务二")
group.leave()
};
group.notify(queue: DispatchQueue.main) {
print("所有任务结束")
}
六、信号量
1、控制线程的最大并发数量
class ViewController: UIViewController {
var semaphore:DispatchSemaphore?
override func viewDidLoad() {
super.viewDidLoad()
semaphore = DispatchSemaphore(value: 3)
for _ in 0...10 {
let thread = Thread(target: self, selector: #selector(test), object: nil)
thread.start()
}
}
@objc func test(){
semaphore?.wait()
sleep(10)
print("测试")
semaphore?.signal()
}
}
2、保证线程安全(控制线程的最大并发数量为1)
class ViewController: UIViewController {
var semaphore:DispatchSemaphore?
var array:[Any] = Array()
override func viewDidLoad() {
super.viewDidLoad()
semaphore = DispatchSemaphore(value: 1)
for _ in 0...10000 {
let thread = Thread(target: self, selector: #selector(test), object: nil)
thread.start()
}
}
@objc func test(){
semaphore?.wait()
array.append(2)
print("测试")
semaphore?.signal()
}
}
3、线程同步
var semaphore:DispatchSemaphore?
var array:[Any] = Array()
override func viewDidLoad() {
super.viewDidLoad()
semaphore = DispatchSemaphore(value: 0)
self.request1()
semaphore?.wait()
self.request2()
}
func request1(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
self.semaphore?.signal()
print("request1")
}
}
func request2(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
print("request2")
}
}
}
这样就保证了request1执行完后才能执行request2
4、多线程依赖
假如request1、request2请求完之后,再请求request3我们就可以这样
class ViewController: UIViewController {
var semaphore:DispatchSemaphore?
var group:DispatchGroup?
var array:[Any] = Array()
override func viewDidLoad() {
super.viewDidLoad()
let group = DispatchGroup()
self.group = group
let semophore = DispatchSemaphore.init(value: 0)
self.semaphore = semophore
let queue = DispatchQueue.global()
queue.async(group: group, execute: {
self.request1()
semophore.wait()
})
queue.async(group: group, execute: {
self.request2()
semophore.wait()
})
group.notify(queue: queue) {
self.request3()
}
}
func request1(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
self.semaphore?.signal()
print("request1")
}
}
}
func request2(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
self.semaphore?.signal()
print("request2")
}
}
}
func request3(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
print("request3")
}
}
}
}
或者这样
class ViewController: UIViewController {
var semaphore:DispatchSemaphore?
var group:DispatchGroup?
var array:[Any] = Array()
override func viewDidLoad() {
super.viewDidLoad()
let group = DispatchGroup()
self.group = group
let semophore = DispatchSemaphore.init(value: 0)
self.semaphore = semophore
let queue = DispatchQueue.global()
queue.async(group: group, execute: {
self.request1()
})
queue.async(group: group, execute: {
self.request2()
})
group.notify(queue: queue) {
semophore.wait()
semophore.wait()
self.request3()
}
}
func request1(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
self.semaphore?.signal()
print("request1")
}
}
}
func request2(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
self.semaphore?.signal()
print("request2")
}
}
}
func request3(){
DispatchQueue.global().asyncAfter(deadline: DispatchTime.now() + Double.random(in: 0...5)
) {
DispatchQueue.main.async {
print("request3")
}
}
}
}
七、多线程开发-once
dispatch_once在Swift中已经被废弃,取而代之可以用类型属性或者全局变量\常量。
fileprivate let inittask2:Void = {
print("inittask2")
}()
class Student{
static let initTask1:Void = {
print("inittask1")
}()
}