1.异步方式执行
import UIKit
class ViewController: UIViewController {
let queue = DispatchQueue(label: "TestQueue", attributes: DispatchQueue.Attributes.concurrent)
override func viewDidLoad() {
super.viewDidLoad()
queue.async {
for _ in 0..<10 {
print("A------async -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("B------async -- \(Thread.current)")
}
}
在异步方式下的执行
queue.async(flags: .barrier) {
for _ in 0..<10 {
print("C------async -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("D------async -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("E------async -- \(Thread.current)")
}
}
}
}
执行结果:
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
A------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
C------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600003180300>{number = 4, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
E------async -- <NSThread: 0x6000031802c0>{number = 3, name = (null)}
image.png
2.同步方式执行
import UIKit
class ViewController: UIViewController {
let queue = DispatchQueue(label: "TestQueue", attributes: DispatchQueue.Attributes.concurrent)
override func viewDidLoad() {
super.viewDidLoad()
queue.async {
for _ in 0..<10 {
print("A------async -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("B------async -- \(Thread.current)")
}
}
// 同步执行
queue.sync(flags: .barrier) {
for _ in 0..<10 {
print("C------sync -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("D------async -- \(Thread.current)")
}
}
queue.async {
for _ in 0..<10 {
print("E------async -- \(Thread.current)")
}
}
}
}
执行结果
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
A------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
B------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
C------sync -- <NSThread: 0x600002b75a00>{number = 1, name = main}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
E------async -- <NSThread: 0x600002b298c0>{number = 3, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
D------async -- <NSThread: 0x600002b23100>{number = 4, name = (null)}
image.png
You can read about them in the dispatch_block_flags_t
manual:
网友评论