Swift GCD詳解

1.延時(shí)器

// 主線程調(diào)用
DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 5.0) {
    print("5 秒后輸出", Thread.current.isMainThread)
}
// 5 秒后輸出 true

2.異步執(zhí)行回主線程寫法

DispatchQueue.global().async {
    print("async do something\(Thread.current)")
    DispatchQueue.main.async {
        print("come back to main thread\(Thread.current)")
    }
}
/*
async do something<NSThread: 0x600002f7e6c0>{number = 3, name = (null)}
come back to main thread<NSThread: 0x600002f27c80>{number = 1, name = main}
*/

3. DispatchGroup(線程組)

let group = DispatchGroup()
// 第一組
group.enter()
AlamofireTools().getAlamofireData(url: "topic/list/jingxuan/1/bs02-iphone-4.6/0-500.json", success: { (json) in
    group.leave()
    print("第一組成功")
}) { (error) in
     group.leave()
    print("第一組失敗")
}

// 第二組
group.enter()
AlamofireTools().getAlamofireData(url: "topic/list/jingxuan/1/bs02-iphone-4.6/0-10.json", success: { (json) in
    group.leave()
    print("第二組成功")
}) { (error) in
    group.leave()
    print("第二組失敗")
}

// 結(jié)束
group.notify(queue: DispatchQueue.main) {
     print("全部執(zhí)行完成")
}
/*
第二組成功
第一組成功
全部執(zhí)行完成
*/

4.信號(hào)量(DispatchSemaphore)

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("第二組失敗")
    }
}


// 結(jié)束
queue.async {
    semaphore.wait()

    AlamofireTools().getAlamofireData(url: "topic/list/jingxuan/1/bs02-iphone-4.6/0-1.json", success: { (json) in
        semaphore.signal()
        print("全部執(zhí)行完成")
    }) { (error) in
        semaphore.signal()
        print("全部執(zhí)行完成")
    }
}
/*
queue.async {
    semaphore.wait()
    DispatchQueue.main.async {
        print("全部執(zhí)行完成")
        semaphore.signal()
    }
}
*/
/*
第一組成功
第二組成功
全部執(zhí)行完成
*/
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Swift1> Swift和OC的區(qū)別1.1> Swift沒有地址/指針的概念1.2> 泛型1.3> 類型嚴(yán)謹(jǐn) 對(duì)...
    cosWriter閱讀 11,684評(píng)論 1 32
  • 原文:http://www.itdecent.cn/p/2d57c72016c6[https://www.jia...
    夜雨聲煩_閱讀 509評(píng)論 0 0
  • 本文用來介紹 iOS 多線程中 GCD 的相關(guān)知識(shí)以及使用方法。這大概是史上最詳細(xì)、清晰的關(guān)于 GCD 的詳細(xì)講...
    花花世界的孤獨(dú)行者閱讀 581評(píng)論 0 1
  • 本文是由于筆者在閱讀有關(guān)多線程的文章的時(shí)候,看到的覺得寫的很好, 就此記錄下. 在開發(fā) APP 的時(shí)候很多時(shí)候可能...
    我太難了_9527閱讀 464評(píng)論 0 2
  • 本文內(nèi)容任務(wù)、隊(duì)列的概念、創(chuàng)建方式任務(wù) + 隊(duì)列的6種組合的執(zhí)行方式線程間如何通信dispatch_once、di...
    小秀秀耶閱讀 1,084評(píng)論 0 9

友情鏈接更多精彩內(nèi)容