如何使用swift中的GCD

這篇文章主要介紹“如何使用swift中的GCD”,在日常操作中,相信很多人在如何使用swift中的GCD問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”如何使用swift中的GCD”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),內(nèi)蒙古企業(yè)網(wǎng)站建設(shè),內(nèi)蒙古品牌網(wǎng)站建設(shè),網(wǎng)站定制,內(nèi)蒙古網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,內(nèi)蒙古網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

Grand Central Dispath(GCD)

GCD是采用任務(wù)+隊(duì)列的方式,有易用、效率高、性能好的特點(diǎn)

//concurrent 并行
let queue = DispatchQueue(
    label: "myQueue",
    qos: DispatchQoS.default,
    attributes: DispatchQueue.Attributes.concurrent,
    autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency.inherit,
    target: nil)

//sync
queue.sync {
    sleep(1)
    print("in queue sync")
}

//async
queue.async {
    sleep(1)
    print("in queue async")
}

//asyncAfter
queue.asyncAfter(deadline: .now() + 1) {
    print("in queue asyncAfter")
}

print("after invoke queue method")

/*
 in queue sync
 after invoke queue method
 in queue async
 in queue asyncAfter
 */

DispatchGroup-wait

//DispatchGroup-wait
let workingGroup = DispatchGroup()
let workingQueue = DispatchQueue(label: "request_queue")

workingGroup.enter()
workingQueue.async {
    sleep(1)
    print("A done")
    workingGroup.leave()
}

workingGroup.enter()
workingQueue.async {
    print("B done")
    workingGroup.leave()
}

print("first")

//workingGroup.wait()
//print("last")
/*
 first
 A done
 B done
 last
 */

DispatchGroup-notify

//DispatchGroup-notify
workingGroup.notify(queue: workingQueue) {
    print("A and B done")
}
print("other")

/*
 first
 other
 A done
 B done
 A and B done
 */

DispatchSource-Timer

//DispatchSource-Timer
var seconds = 10
let timer: DispatchSourceTimer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
timer.schedule(deadline: .now(), repeating: 1.0)
timer.setEventHandler {
    seconds -= 1
    if seconds < 0 {
        timer.cancel()
    } else {
        print(seconds)
    }
}
timer.resume()
/*
 9
 8
 7
 6
 5
 4
 3
 2
 1
 0
 */

實(shí)現(xiàn)一個(gè)線程安全的Array的讀和寫

//實(shí)現(xiàn)一個(gè)線程安全的Array的讀和寫
var array = Array(0...1000)
let lock = NSLock()

func getLastItem() -> Int? {
    lock.lock()
    var temp: Int? = nil
    if array.count > 0 {
        temp = array[array.count - 1]
    }
    lock.unlock()
    return temp
}

func removeLastItem() {
    lock.lock()
    array.removeLast()
    lock.unlock()
}

queue.async {
    for _ in 0..<1000{
        removeLastItem()
    }
}

queue.async {
    for _ in 0..<1000 {
        if let item = getLastItem() {
            print(item)
        }
    }
}

到此,關(guān)于“如何使用swift中的GCD”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!

本文標(biāo)題:如何使用swift中的GCD
網(wǎng)頁(yè)鏈接:http://muchs.cn/article8/ihipop.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)網(wǎng)站制作、手機(jī)網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì)、網(wǎng)站改版自適應(yīng)網(wǎng)站Google

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設(shè)