IOS之GCD詳細步驟

夢筆隨心發表於2016-05-20

#pragma mark 序列同步

    /*

    //1.建立一個序列佇列

    dispatch_queue_t queueSerialSync = dispatch_queue_create("queueSerialSync", DISPATCH_QUEUE_SERIAL);

    //2. 在佇列中同步執行 不具有開啟新執行緒的能力

    dispatch_sync(queueSerialSync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_sync(queueSerialSync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_sync(queueSerialSync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

     */

#pragma mark 序列非同步

    /*

    //1.建立一個序列佇列

    dispatch_queue_t queueSerialAsync =dispatch_queue_create("queueSerialAsync", DISPATCH_QUEUE_SERIAL);

    //2. 在佇列中同步執行 不具有開啟新執行緒的能力

    dispatch_async(queueSerialAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_async(queueSerialAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_async(queueSerialAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

     */

#pragma mark 並行同步

    /*

    //1.建立一個並行佇列 可有多個執行緒

    dispatch_queue_t queueConcurrentSync = dispatch_queue_create("queueConcurrentSync", DISPATCH_QUEUE_CONCURRENT);

    //2. 在佇列中同步執行 不具有開啟新執行緒的能力

    dispatch_sync(queueConcurrentSync, ^{

        NSLog(@"%@",[NSThread currentThread]);


    });

    dispatch_sync(queueConcurrentSync, ^{

        NSLog(@"%@",[NSThread currentThread]);

        

    });

    dispatch_sync(queueConcurrentSync, ^{

        NSLog(@"%@",[NSThread currentThread]);

        

    });

     */

#pragma mark 並行非同步

    

    //1.建立一個並行佇列 可有多個執行緒 (這是系統裡面封裝的一個並行佇列)

    dispatch_queue_t queueConcurrentAsync = dispatch_get_global_queue(0, 0);

    /*

     四種優先順序 從上到下降低

     *  - DISPATCH_QUEUE_PRIORITY_HIGH:         QOS_CLASS_USER_INITIATED

     *  - DISPATCH_QUEUE_PRIORITY_DEFAULT:      QOS_CLASS_DEFAULT

     *  - DISPATCH_QUEUE_PRIORITY_LOW:          QOS_CLASS_UTILITY

     *  - DISPATCH_QUEUE_PRIORITY_BACKGROUND:   QOS_CLASS_BACKGROUND

    */

    /*

    //2. 非同步方式執行

    dispatch_async(queueConcurrentAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

        

    });

    dispatch_async(queueConcurrentAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

        

    });

    dispatch_async(queueConcurrentAsync, ^{

        NSLog(@"%@",[NSThread currentThread]);

        

    });

     */

    

#pragma 獲取到主佇列

    //dispatch_queue_t mainQueue = dispatch_get_main_queue();

#pragma  GCD 應用

    //找到全域性的並行佇列

    dispatch_queue_t content = dispatch_get_global_queue(0, 0);

    //找到組

    dispatch_group_t group = dispatch_group_create();

    //新增任務

    dispatch_group_async(group, content, ^{

       NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_group_async(group, content, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    dispatch_group_async(group, content, ^{

        NSLog(@"%@",[NSThread currentThread]);

    });

    //監聽完成 上面任務全部完成後觸發

    dispatch_group_notify(group, content, ^{

        NSLog(@"任務完成");

    });

相關文章