/**
* 同步执行 + 并发队列
* 特点:不会开启新线程,在当前线程中执行任务,执行完一个任务,再执行下一个任务。
*/
- (void)syncConcurrent {
//打印当前线程
NSLog(@"currentThread---%@",[NSThread currentThread]);
NSLog(@"syncConcurrent---begin");
dispatch_queue_t queue = dispatch_queue_create("net.bujige.testQueue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
// 追加任务1
for (int i = 0; i < 2; ++i) {
//模拟耗时操作
[NSThread sleepForTimeInterval:4];
//打印当前线程
NSLog(@"1---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任务2
for (int i = 0; i < 2; ++i) {
//模拟耗时操作
[NSThread sleepForTimeInterval:3];
//打印当前线程
NSLog(@"2---%@",[NSThread currentThread]);
}
});
dispatch_sync(queue, ^{
// 追加任务3
for (int i = 0; i < 2; ++i) {
//模拟耗时操作
[NSThread sleepForTimeInterval:2];
//打印当前线程
NSLog(@"3---%@",[NSThread currentThread]);
}
});
NSLog(@"syncConcurrent---end");
}
运行结果:
2020-07-06 20:35:47.995878+0800 GCD[15831:3005855] currentThread---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:35:47.995997+0800 GCD[15831:3005855] syncConcurrent---begin
2020-07-06 20:35:51.996729+0800 GCD[15831:3005855] 1---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:35:55.997665+0800 GCD[15831:3005855] 1---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:35:58.998527+0800 GCD[15831:3005855] 2---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:36:01.999486+0800 GCD[15831:3005855] 2---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:36:03.999725+0800 GCD[15831:3005855] 3---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:36:06.000605+0800 GCD[15831:3005855] 3---<NSThread: 0x600000f8d040>{number = 1, name = main}
2020-07-06 20:36:06.000765+0800 GCD[15831:3005855] syncConcurrent---end
网友评论