优点:
(1)易用:GCD比thread更简单易用。基于block的特效使它能极为简单地在不同代码作用域之间传递上下文。
(2)效率:GCD实现功能轻量,优雅,使得它在很多地方比专门创建消耗资源的线程更加实用且快捷。
(3)性能:GCD自动根据系统负载来增减线程数量,从而减少了上下文切换并增加了计算效率。
(4)安全:无需加锁或其他同步机制
多线程的四种方式
1,串行 同步
func test1() {
let test1queue = DispatchQueue.init(label: "test1que")
test1queue.sync {
for i in 0..<2 {
print("\(i),\(Thread.current)")
}
print("第一个结束\n-------------------------")
}
test1queue.sync {
for i in 2..<4 {
print("\(i),\(Thread.current)")
}
print("第二个结束\n-------------------------")
}
test1queue.sync {
for i in 4..<6 {
print("\(i),\(Thread.current)")
}
print("第三个结束\n-------------------------")
}
}
打印结果:
0,<NSThread: 0x280942040>{number = 1, name = main}
1,<NSThread: 0x280942040>{number = 1, name = main}
第一个结束
-------------------------
2,<NSThread: 0x280942040>{number = 1, name = main}
3,<NSThread: 0x280942040>{number = 1, name = main}
第二个结束
-------------------------
4,<NSThread: 0x280942040>{number = 1, name = main}
5,<NSThread: 0x280942040>{number = 1, name = main}
第三个结束
-------------------------
串行同步: 顺序执行 不开启新线程
2,串行 异步
func test2() {
let test1queue = DispatchQueue.init(label: "test1que")
test1queue.async {
for i in 0..<2 {
print("\(i),\(Thread.current)")
}
print("第一个结束\n-------------------------")
}
test1queue.async {
for i in 2..<4 {
print("\(i),\(Thread.current)")
}
print("第二个结束\n-------------------------")
}
test1queue.async {
for i in 4..<6 {
print("\(i),\(Thread.current)")
}
print("第三个结束\n-------------------------")
}
}
打印结果:
0,<NSThread: 0x283954bc0>{number = 6, name = (null)}
1,<NSThread: 0x283954bc0>{number = 6, name = (null)}
第一个结束
-------------------------
2,<NSThread: 0x283954bc0>{number = 6, name = (null)}
3,<NSThread: 0x283954bc0>{number = 6, name = (null)}
第二个结束
-------------------------
4,<NSThread: 0x283954bc0>{number = 6, name = (null)}
5,<NSThread: 0x283954bc0>{number = 6, name = (null)}
第三个结束
-------------------------
串行异步: 顺序执行 开启新线程(只开启一条线程)
3,并发 同步
func test3() {
let test1queue = DispatchQueue(label: "test1que", attributes: .concurrent)
test1queue.sync {
for i in 0..<2 {
print("\(i),\(Thread.current)")
}
print("第一个结束\n-------------------------")
}
test1queue.sync {
for i in 2..<4 {
print("\(i),\(Thread.current)")
}
print("第二个结束\n-------------------------")
}
test1queue.sync {
for i in 4..<6 {
print("\(i),\(Thread.current)")
}
print("第三个结束\n-------------------------")
}
}
打印结果:
0,<NSThread: 0x2817aadc0>{number = 1, name = main}
1,<NSThread: 0x2817aadc0>{number = 1, name = main}
第一个结束
-------------------------
2,<NSThread: 0x2817aadc0>{number = 1, name = main}
3,<NSThread: 0x2817aadc0>{number = 1, name = main}
第二个结束
-------------------------
4,<NSThread: 0x2817aadc0>{number = 1, name = main}
5,<NSThread: 0x2817aadc0>{number = 1, name = main}
第三个结束
-------------------------
并发同步: 顺序执行 不开新线程
4,并发 异步(也是用的最多的一种)
func test4() {
let test1queue = DispatchQueue(label: "test1que", attributes: .concurrent)
for i in 0...50 {
test1queue.async {
print("\(i),\(Thread.current)----------------")
}
}
}
打印结果:(最后10个)
40,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
41,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
42,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
43,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
44,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
45,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
46,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
47,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
48,<NSThread: 0x281b2f1c0>{number = 4, name = (null)}----------------
8,<NSThread: 0x281b32780>{number = 6, name = (null)}----------------
16,<NSThread: 0x281b1c140>{number = 7, name = (null)}----------------
17,<NSThread: 0x281b1c200>{number = 8, name = (null)}----------------
32,<NSThread: 0x281b18000>{number = 9, name = (null)}----------------
33,<NSThread: 0x281b180c0>{number = 10, name = (null)}----------------
49,<NSThread: 0x281b18900>{number = 11, name = (null)}----------------
50,<NSThread: 0x281b1ccc0>{number = 12, name = (null)}----------------
并发异步: 无序执行 开启n条线程
队列获取方式
//创建串行队列
let serial = DispatchQueue(label: "serialQueue")
//创建并行队列
let concurrent = DispatchQueue(label: "concurrentQueue", attributes: .concurrent)
//获取全局队列
let globalQueue = DispatchQueue.global(qos: .default)
//获取主线程队列
let mainQueue = DispatchQueue.main//一般跟UI相关的操作都放在主线程队列