swift3.0 - 信号量、group、barrier

2017-03-14  本文已影响61人  GA_
//
//  ViewController.swift
//  并发
//
//  Created by houjianan on 2017/2/27.
//  Copyright © 2017年 houjianan. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 信号量和group
        // 各组执行完才会执行notify
        // 使用信号量 当异步延迟执行完才执行notify
        // 如果没有信号量控制 6的打印就是正常延迟3秒后执行
        let queue = DispatchQueue.global(qos: .default)
        let group = DispatchGroup()
        let sem = DispatchSemaphore(value: 0)
        queue.async(group: group, execute: {
            print(1)
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                sem.signal()
                print(6)
            })
            sem.wait()
        })
        queue.async(group: group, execute: {
            print(2)
        })
        queue.async(group: group, execute: {
            print(3)
        })
        group.notify(queue: queue, execute: {
            print(5)
        })

        // group的enter()和leave()使用
        // 异步延迟方法都执行完执行notify
        let queue = DispatchQueue.global(qos: .default)
        let group = DispatchGroup()
        group.enter()
        queue.async(group: group, execute: {
            print(1)
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                print(6)
                group.leave()
            })
        })
        group.enter()
        queue.async(group: group, execute: {
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                print(2)
                group.leave()
            })
        })
        group.enter()
        queue.async(group: group, execute: {
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                print(3)
                group.leave()
            })
        })
        group.notify(queue: queue, execute: {
            print(4)
        })

        // group和信号量控制异步顺序执行
        let queue = DispatchQueue.global(qos: .default)
        let group = DispatchGroup()
        let sem = DispatchSemaphore(value: 0)
        queue.async(group: group, execute: {
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                sem.signal()
                print(1)
                sem.wait()
            })
        })
        queue.async(group: group, execute: {
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
            sem.signal()
                print(2)
                sem.wait()
            })
            
        })
        queue.async(group: group, execute: {
            queue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                sem.signal()
                print(3)
                sem.wait()
            })
        })
        group.notify(queue: queue, execute: {
            sem.signal()
            print(7)
            sem.wait()
        })

        // 标记barrier的线程不执行完 后面的不会执行
        let dataQueue = DispatchQueue(label: "data", attributes: .concurrent)
        let write = DispatchWorkItem(flags: .barrier) {
            dataQueue.asyncAfter(deadline: DispatchTime.now() + Double(Int64(3 * 1000 * 1000000)) / Double(NSEC_PER_SEC), execute: {
                print(2.1)
            })
            print(2)
            Thread.sleep(forTimeInterval: 5)
        }
        
        dataQueue.async {
            print(1)
        }
        
        dataQueue.async(execute: write)
        
        dataQueue.async {
            print(3)
        }
    }
}
上一篇下一篇

猜你喜欢

热点阅读