美文网首页
DispatchWorkItemFlags.barrier

DispatchWorkItemFlags.barrier

作者: ngugg | 来源:发表于2018-10-12 10:20 被阅读10次

    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:

    相关文章

      网友评论

          本文标题:DispatchWorkItemFlags.barrier

          本文链接:https://www.haomeiwen.com/subject/tsywaftx.html