swift 와 oc gcd 사용

4805 단어 IOS
    oc swift  ,  swift gcd   ,    gcd    ,        
///     +     ,        ,      
    func asyncConcurrent() {
        /* swift */
        print("asyncConcurrent---begin");
        let queue = DispatchQueue(label: "com.lin.syc")
        queue.async {
            for i in 0..<2 {
                print("-------",i)
            }
        }
        queue.async {
            for i in 3..<5 {
                print("-------",i)
            }
        }
        /* oc */
//        dispatch_queue_t queue= dispatch_queue_create("test.queue", DISPATCH_QUEUE_CONCURRENT);
//
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"1------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"2------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_async(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"3------%@",[NSThread currentThread]);
//            }
//            });
        print("asyncConcurrent---end");
    }
    
    ///     +     ,       ,         。      ,       ,        
    func syncSerial() {
        print("syncSerial---begin");
        /* swift */
        let queue = DispatchQueue(label: "com.lin.syc")
        queue.sync {
            for i in 0..<2 {
                print("-------",i)
            }
        }
        queue.sync {
            for i in 3..<5 {
                print("-------",i)
            }
        }
        /* oc */
//        dispatch_queue_t queue = dispatch_queue_create("test.queue", DISPATCH_QUEUE_SERIAL);
//
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"1------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"2------%@",[NSThread currentThread]);
//            }
//            });
//        dispatch_sync(queue, ^{
//            for (int i = 0; i < 2; ++i) {
//                NSLog(@"3------%@",[NSThread currentThread]);
//            }
//            });
        print("syncSerial---end");
    }
    
    ///            ,       ,            
    func barrier() {
        /* swift */
        let queue = DispatchQueue(label: "com.lin.syc")
        queue.async {
            print("----1---",Thread.current)
        }
        queue.async {
            print("----2---",Thread.current)
        }
        queue.async(group: nil, qos: .default, flags: .barrier) {
            print("----3---",Thread.current)
        }
        queue.async {
            print("----4---",Thread.current)
        }
        /* oc */
//        dispatch_queue_t queue = dispatch_queue_create("12312312", DISPATCH_QUEUE_CONCURRENT);
//
//        dispatch_async(queue, ^{
//            NSLog(@"----1-----%@", [NSThread currentThread]);
//            });
//        dispatch_async(queue, ^{
//            NSLog(@"----2-----%@", [NSThread currentThread]);
//            });
//
//        dispatch_barrier_async(queue, ^{
//            NSLog(@"----barrier-----%@", [NSThread currentThread]);
//            });
//
//        dispatch_async(queue, ^{
//            NSLog(@"----3-----%@", [NSThread currentThread]);
//            });
//        dispatch_async(queue, ^{
//            NSLog(@"----4-----%@", [NSThread currentThread]);
//            });
    }
    
    ///      2     ,   2                     
    func group() {
        /* swift */
        let group = DispatchGroup()
        let queue = DispatchQueue(label: "com.lin.syc")
        queue.async(group: group, qos: .default, flags: []) {
            print("----1---",Thread.current)
        }
        queue.async(group: group, qos: .default, flags: []) {
            print("----2---",Thread.current)
        }
        group.notify(queue: .main) {
            
        }
        /* oc */
//        dispatch_group_t group =  dispatch_group_create();
//
//        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            //   1        
//            });
//
//        dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//            //   1        
//            });
//
//        dispatch_group_notify(group, dispatch_get_main_queue(), ^{
//            //               ,     ...
//            });
    }

좋은 웹페이지 즐겨찾기