Swift -GCD

1、创建queue

let queue = DispatchQueue.init(label: "labelName", qos: DispatchQoS.default, attributes: DispatchQueue.Attributes.init(), autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit, target: nil)

let concurrentQueue = DispatchQueue(label: "labelName", attributes: .concurrent)

queue.async {
      print("------- ")
}

参数解释:label:对列标识;qos:队列优先级;attributes:队列类型,默认是串行队列;autoreleaseFrequency:队列释放的频率

2、信号量

let semaphore = DispatchSemaphore(value: 1)
let queue = DispatchQueue(label: "L?II")
// 第一组
queue.async {
     semaphore.wait()
    AlamofireTools().getAlamofireData(url: "topic/list/jingxuan/1/bs02-iphone-4.6/0-100.json", success: { (json) in
        semaphore.signal()
        print("第一组成功")
    }) { (error) in
        semaphore.signal()
        print("第一组失败")
    }
}
// 第二组
queue.async {
    semaphore.wait()
    AlamofireTools().getAlamofireData(url: "topic/list/jingxuan/1/bs02-iphone-4.6/0-100000.json", success: { (json) in
        semaphore.signal()
        print("第二组成功")
    }) { (error) in
        semaphore.signal()
        print("第二组失败")
    }
}
queue.async {
    semaphore.wait()
    DispatchQueue.main.async {
        print("全部执行完成")
        semaphore.signal()
    }
}
/*
第一组成功
第二组成功
全部执行完成
*/

3、组group,等前面的执行结束,notify才执行,不会造成线程堵塞

 let group = DispatchGroup.init()
concurrentQueue.async(group: group, qos: .default, flags: []) {
            for i in 0 ... 3 {
                print("------- \(i)")
            }
        }
        concurrentQueue.async(group: group, qos: .default, flags: []) {
            for i in 0 ... 3 {
                print("----======--- \(i)")
            }
        }
        
        group.notify(queue: concurrentQueue) {
            print("前面执行完毕")
        }
        queue.async(group: group, qos: .default, flags: []) {
            print("执行结束-----")
        }
        queue.async {
            print("执行结束")
        }
打印结果:::
执行结束-----
执行结束
------- 0
----======--- 0
------- 1
------- 2
------- 3
----======--- 1
----======--- 2
----======--- 3
前面执行完毕
// wait:会造成线程堵塞,前面的执行完毕或者超过限制时间就执行下面任务
        let group = DispatchGroup.init()
        concurrentQueue.async(group: group, qos: .default, flags: []) {
            for i in 0 ... 3 {
                print("------- \(i)")
            }
        }
        concurrentQueue.async(group: group, qos: .default, flags: []) {
            for i in 0 ... 3 {
                print("----======--- \(i)")
            }
        }
let result = group.wait(timeout: DispatchTime.now() + 2)
        switch result {
            case .success:
                print("前面执行完毕")
            case .timedOut:
                print("前面执行超时")
        }
        queue.async(group: group, qos: .default, flags: []) {
            print("执行结束-----")
        }
        queue.async {
            print("执行结束")
        }

----======--- 0
----======--- 1
----======--- 2
----======--- 3
------- 0
------- 1
------- 2
------- 3
前面执行完毕
执行结束-----
执行结束

4、栅栏barrier,会造成线程堵塞

queue.async {
            for i in 0 ... 3 {
                print("------- \(i)")
            }
        }
        queue.async {
            for i in 0 ... 3 {
                print("---======---- \(i)")
            }
        }

        queue.async(group: nil, qos: .default, flags: DispatchWorkItemFlags.barrier) {
            print("+++++++++")
        }
        queue.async {
            print("lllllll")
        }
打印结果:
------- 0
------- 1
------- 2
------- 3
---======---- 0
---======---- 1
---======---- 2
---======---- 3
+++++++++
lllllll

5、DispatchWorkItem :代替() -> Void 包,有wait(堵塞线程)和notify(不堵塞线程)函数,和group用法相识

let workItem = DispatchWorkItem.init {
            Thread.sleep(forTimeInterval: TimeInterval.init(2))
            print("执行该任务")
        }
        
        concurrentQueue.async(execute: workItem)
//        workItem.wait(timeout: DispatchTime.now() + 2)
        workItem.notify(queue: concurrentQueue) {
            print("workItem执行完毕")
        }
        print("workItem执行完毕---")

6、asyncAfter:不是执行的延迟时间,是添加到队列的延迟时间

        concurrentQueue.asyncAfter(deadline: DispatchTime.now() + 3, execute: {
            print("-------111 ")
            semaphore.signal()
        })

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