【ios】多线程编程基础 -- GCD

基础

首先推荐一篇比较简单易懂的多线程编程与异步的基础介绍,可以对ios的多线程有个简单的了解:https://juejin.im/post/5a309c525188255ea95befef

官方文档(个人感觉不适合零基础的同学,作为初学者的我看的有点痛苦😭):线程编程指南:https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/Multithreading/ThreadSafety/ThreadSafety.html#//apple_ref/doc/uid/10000057i-CH8-124887

GCD使用

先放常用的,其它用到的后续慢慢补充😑
参照:https://medium.com/@KentaKodashima/swift-grand-central-dispatch-gcd-80bcb16a147f
https://www.jianshu.com/p/d5cc4768034c

附一篇OC语法的GCD使用,也可以做一个参考学习:https://juejin.im/post/5a90de68f265da4e9b592b40

场景1:

  1. 在指定时间后,将block添加到主队列(常用于处理定时器相关UI任务)
  2. 在异步队列中添加耗时久的任务,且任务完成后,需要修改UI
  3. 在某一个任务处理期间,不允许其它任务进行
  4. 以任务组的形式添加任务,并在任务全部完成时,通知另一个队列(通常是主线程队列)

代码

GCDTest.swift:

import Foundation

class GCDTest{
    public init(){ }
    
    // 1. 在指定时间后,将block添加到主队列(常用于处理定时器相关UI任务)
    public func testDispatchMain(){
        print("testDispatchMain begin!")
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now()+3, execute: {
                print("this is dispatch main! testDispatchMain End")
        })
    }
    
    // 2. 在异步队列中添加耗时久的任务,且任务完成后,需要修改UI
    public func testDispatchAsynAndMain(){
        for i in 1...5{
            DispatchQueue(label: "test").async {
                print("this is dispatch asyn!\(i)")
                DispatchQueue.main.async {
                    print("this is dispatch main! asyn")
                }
            }
        }
    }
    
    // 3. 在某一个任务处理期间,不允许其它任务进行(2.和3.用的是同一个队列,但是由于3增加了barrier属性,从而使2.的任务和3.的任务隔离开 -- 如果没有添加这个属性的话,两者的完成顺序不定)
    public func testDispatchBarrier(){
        DispatchQueue(label: "test", attributes: .concurrent).async(flags: .barrier) {
            print("this is dispatch barrier!")
            DispatchQueue.main.async {
                print("this is dispatch main! barrier")
            }
        }
    }
    
    //4. 以任务组的形式添加任务,并在任务全部完成时,通知另一个队列(通常是主线程队列)
    public func testDispatchGroup(){
        let dispatchGroup = DispatchGroup()
        for i in 1...4{
            DispatchQueue(label: "testGroupQue\(i)").async(group: dispatchGroup) {
                print("this is dispatch group\(i)!")
            }
        }
        
        dispatchGroup.notify(queue: .main) {
            print( "All tasks has been completed 😋😋😋")
        }
    }
}

调用:

		let gcdTest = GCDTest()
        gcdTest.testDispatchMain()
        gcdTest.testDispatchGroup()
        gcdTest.testDispatchBarrier()
        gcdTest.testDispatchAsynAndMain()

运行结果:

testDispatchMain begin!
this is dispatch group1!
this is dispatch group3!
this is dispatch group2!
this is dispatch group4!
this is dispatch barrier!
this is dispatch asyn!1
this is dispatch asyn!2
this is dispatch asyn!3
this is dispatch asyn!4
this is dispatch asyn!5
All tasks has been completed 😋😋😋
this is dispatch main! barrier
this is dispatch main! asyn
this is dispatch main! asyn
this is dispatch main! asyn
this is dispatch main! asyn
this is dispatch main! asyn
this is dispatch main! testDispatchMain End
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值