GCD的四種隊列,兩種函數和六種組合

//    四種隊列

    //————————————————————————————————————————————————————————————————————————————————

//    //1、主隊列(是串行隊列)

//    dispatch_queue_t mainQueue = dispatch_get_main_queue();

//    

//    //2、全局並行隊列

//    dispatch_queue_t concu = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);//兩個參數,前者是優先級,後者目前用不到

//    

//    //3、創建串行隊列

//    dispatch_queue_t queueSerial = dispatch_queue_create("je", DISPATCH_QUEUE_SERIAL);//兩個參數,前者是名字(注意是c字符串),後者是隊列類型。

//    

//    //4、創建並行隊列(一般使用系統帶的全局並行隊列即可)

//    dispatch_queue_t queueConcu = dispatch_queue_create("jr2", DISPATCH_QUEUE_CONCURRENT);

    

    

    

    

//    六種組合

    //————————————————————————————————————————————————————————————————————————————————

    //1>同步執行+串行隊(不會開闢子線程)

//    dispatch_sync(queueSerial, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

//    

//    dispatch_sync(queueSerial, ^{

//        [NSThread sleepForTimeInterval:2];

//

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

//    });

    

    //2>同步執行+並行隊列(不會開闢子線程)

//    dispatch_sync(queueConcu, ^{

//        [NSThread sleepForTimeInterval:2];

//

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

//    });

//    

//    dispatch_sync(queueConcu, ^{

//        [NSThread sleepForTimeInterval:2];

//

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

//    });

    

    

    //3、異步執行+串行隊列(開啓一條子線程,且順序執行)

//    dispatch_async(queueSerial, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

//    

//    dispatch_async(queueSerial, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

    

    //4、異步執行+並行隊列(開啓多條子線程,且併發執行)

//    dispatch_async(queueConcu, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

//    

//    dispatch_async(queueConcu, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

    

    //5、異步執行+全局並行隊列(開啓多條子線程,且併發執行)

//    dispatch_async(concu, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

//    

//    dispatch_async(concu, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

    

    //6、(在主線程中)同步執行+main隊列(死鎖)NSLog添加到了主隊列的最後,NSLog的執行需要等待主隊列執行完之後執行,而主隊列又在等NSLog執行完(注意與情況3的比較)

//    dispatch_sync(mainQueue, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });

//    

//    dispatch_sync(mainQueue, ^{

//        [NSThread sleepForTimeInterval:2];

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

//    });


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章