Swift3GCD

GCD的使用在Swift3中的方法

//串行队列

let q:DispatchQueue = DispatchQueue(label: "xiaosi")

 

//并发队列 qos : 为 优先级别 可以不设置

let queue = DispatchQueue(label: "com.appcoda.anotherQueue", qos: .userInitiated, attributes: .concurrent)

 

使用最多的是异步并发

        queue.async {

            print("异步并发11111\(Thread.current)")

        }

        

        queue.async {

            print("异步并发22222\(Thread.current)")

        }

        

        queue.async {

            print("异步并发33333\(Thread.current)")

        }

        

        queue.async {

            print("异步并发44444\(Thread.current)")

        }

 

其次是异步串行

        queue.async {

            print("异步串行11111\(Thread.current)")

        }

        queue.async {

            print("异步串行2222\(Thread.current)")

        }

        queue.async {

            print("异步串行4444\(Thread.current)")

        }

        queue.async {

            print("异步串行5555\(Thread.current)")

        }

还有就是线程组

        let group = DispatchGroup()

 

        q.async(group: group) {

            sleep(2)

            print("异步任务3333\(Thread.current)")

        }

        

        q.async(group: group) {

            sleep(2)

            print("异步任务4444\(Thread.current)")

        }

        

        group.notify(queue: q) {

            print("全部做完了")

        }

        

 

 

 

转载于:https://www.cnblogs.com/xsiOS/p/6179474.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值