swift3.0-信号量、group、barrier

3921 ワード

//
//  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)
        }
    }
}