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]);//    });
作为一个开发者,有一个学习的气氛跟一个交换圈子特地重要,这是一个我的iOS交换群:642363427不论你是小白还是大牛欢送入驻 ,分享BAT,阿里面试题、面试教训,探讨技术, 大家一起交流学习成长!