swift GCD代码

//dispatch_async
        let mainQueue: dispatch_queue_t = dispatch_get_main_queue()
        dispatch_async(mainQueue, {() -> Void in
            print("\nthe queue is : \(NSThread.currentThread())")
        })
         
        let backgroundQueue: dispatch_queue_t = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0)
        dispatch_async(backgroundQueue, {() -> Void in
            print("\nthe queue is : \(NSThread.currentThread())")
        })
         
        //dispatch_after
        let time = dispatch_time(DISPATCH_TIME_NOW, (Int64)(10*NSEC_PER_SEC))
        dispatch_after(time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {() -> Void in
            print("\ntime is : 10")
        })
         
        //dispatch_group
        let globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        let group = dispatch_group_create()
        dispatch_group_async(group, globalQueue, {
            () -> Void in
            print("group 1")
        })
         
        dispatch_group_async(group, globalQueue, {
            () -> Void in
            print("group 2")
        })
         
        dispatch_group_async(group, globalQueue, {
            () -> Void in
            print("group 3")
        })
         
//        dispatch_group_notify(group, globalQueue, {
//            () -> Void in
//            print("completed")
//        })
        dispatch_group_wait(group, DISPATCH_TIME_FOREVER)
        print("\ndispatch_group completed")
         
        //dispatch_barrier_async
        NSUserDefaults.standardUserDefaults().setInteger(9, forKey: "Integer_Key")
        let barrierQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_barrier_async(barrierQueue){
            self.writeFile();
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
        dispatch_async(barrierQueue){
            self.readFile()
        }
         
        //dispatch_apply
        let applyQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        dispatch_async(applyQueue, {
            dispatch_apply(10, applyQueue, {
                (index) -> Void in
                print("\n")
                print(index)
            })
            print("\ndispatch_apply completed")
        })
        print("\n在dispatch_apply之前")
         
        //dispatch_semaphore
        let semaphoreQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
        let semaphore = dispatch_semaphore_create(1)
        for _ in 0 ... 9 {
            dispatch_async(semaphoreQueue, {
                () -> Void in
                dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER)
                let time = dispatch_time(DISPATCH_TIME_NOW, (Int64)(NSEC_PER_SEC*2))
                dispatch_after(time, semaphoreQueue, {
                    print("\n2秒后执行")
                    dispatch_semaphore_signal(semaphore)
                })
            })
        }

class var sharedInstance: ViewController {
        struct Static {
            static var onceToken : dispatch_once_t = 0
            static var instance : ViewController? = nil
        }
        dispatch_once(&Static.onceToken, {
            Static.instance = ViewController()
        })
        return Static.instance!
    }
`````````

你可能感兴趣的:(swift GCD代码)