Swift3GCD

时间:2022-03-30 06:45:56

GCD的使用在Swift3中的方法

//串行队列

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

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

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
span.s1 { }
span.s2 { color: #bb2ca2 }
span.s3 { color: #703daa }
span.s4 { color: #d12f1b }

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)")

}

还有就是线程组

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa }
span.s1 { color: #bb2ca2 }
span.s2 { color: #000000 }
span.s3 { }

let group = DispatchGroup()

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px }
span.s1 { }
span.s2 { color: #3d1d81 }
span.s3 { color: #272ad8 }
span.s4 { color: #d12f1b }
span.s5 { font: 11.0px "PingFang SC"; color: #d12f1b }
span.s6 { color: #703daa }

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("全部做完了")

}

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
span.s1 { }
span.s2 { color: #3d1d81 }
span.s3 { color: #d12f1b }
span.s4 { font: 11.0px "PingFang SC"; color: #d12f1b }
span.s5 { color: #703daa }

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #703daa }
span.s1 { color: #bb2ca2 }
span.s2 { color: #000000 }
span.s3 { }
span.s4 { color: #d12f1b }
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
span.s1 { color: #bb2ca2 }
span.s2 { }
span.s3 { color: #703daa }
span.s4 { color: #d12f1b }
p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px "PingFang SC"; color: #008400 }
p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo }
p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; min-height: 13.0px }
span.s1 { font: 11.0px Menlo }
span.s2 { }
span.s3 { color: #3d1d81 }
span.s4 { color: #d12f1b }
span.s5 { font: 11.0px "PingFang SC"; color: #d12f1b }
span.s6 { color: #703daa }

相关文章