-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self syncConcurrent];
}
- 同步函数+主队列 没有开启新的线程 串行执行任务 会造成线程阻塞 你等我来我等你结果就耗着了
-(void)syncMain{
//1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
NSLog(@"%s",__func__);
//2、将任务加进队列
dispatch_sync(queue, ^{
NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2----%@",[NSThread currentThread]);
});
/*2016-07-27 22:03:18.257 GCD[1474:205778] -[ViewController syncMain]*/
}
- 异部函数+主队列 只在主线程中执行任务,没有开启新线程 串行执行任务
-(void)asyncMain{
//1、获得主队列
dispatch_queue_t queue = dispatch_get_main_queue();
//2、将任务添加进队列
dispatch_async(queue, ^{
NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"3----%@",[NSThread currentThread]);
});
/*
2016-07-27 22:01:12.524 GCD[1461:204697] 1----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
2016-07-27 22:01:12.525 GCD[1461:204697] 2----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
2016-07-27 22:01:12.526 GCD[1461:204697] 3----<NSThread: 0x7fe0d0606430>{number = 1, name = main}
*/
}
- 同步函数+串行队列 开启新的线程 串行执行任务
-(void)syncSerial{
//1、获得串行队列
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL);
//2、将任务添加到队列中
dispatch_sync(queue, ^{
NSLog(@"1----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2----%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3----%@",[NSThread currentThread]);
});
/*
2016-07-27 22:09:31.890 GCD[1509:209789] 1----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
2016-07-27 22:09:31.891 GCD[1509:209789] 2----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
2016-07-27 22:09:31.892 GCD[1509:209789] 3----<NSThread: 0x7fcb3b506770>{number = 1, name = main}
*/
}
-
异部函数 + 串行队列 :会开启新的线程,但是任务是串行的,执行完一个任务,在执行下一个任务
-(void)asyncSerial{ dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ NSLog(@"1----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"2----%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"3----%@",[NSThread currentThread]); }); /* 2016-07-27 22:11:47.582 GCD[1531:211558] 1----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)} 2016-07-27 22:11:47.584 GCD[1531:211558] 2----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)} 2016-07-27 22:11:47.585 GCD[1531:211558] 3----<NSThread: 0x7fbc90c0ba50>{number = 2, name = (null)} */ }
-
同步函数 + 并行队列 :不会开启新的线程,串行执行任务
-(void)syncConcurrent{
dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT);
dispatch_sync(queue, ^{
NSLog(@"1-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"2-------%@",[NSThread currentThread]);
});
dispatch_sync(queue, ^{
NSLog(@"3-------%@",[NSThread currentThread]);
});
/*
2016-07-27 22:20:11.875 GCD[1560:215430] 1-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
2016-07-27 22:20:11.877 GCD[1560:215430] 2-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
2016-07-27 22:20:11.878 GCD[1560:215430] 3-------<NSThread: 0x7f9d3af02e70>{number = 1, name = main}
*/
}
-
异步函数 + 并行队列:有开启新的线程,并发执行任务
-(void)asyncConcurrent{ dispatch_queue_t queue = dispatch_queue_create("com.mudy.queue", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ NSLog(@"1-------%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"2-------%@",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"3-------%@",[NSThread currentThread]); }); /* 2016-07-27 22:17:14.859 GCD[1548:213931] 1-------<NSThread: 0x7faaf170fcd0>{number = 5, name = (null)} 2016-07-27 22:17:14.860 GCD[1548:213866] 2-------<NSThread: 0x7faaf140b5a0>{number = 4, name = (null)} 2016-07-27 22:17:14.860 GCD[1548:213860] 3-------<NSThread: 0x7faaf14069e0>{number = 2, name = (null)} */ }
并发队列 | 手动创建的串行队列 | 主队列 | |
---|---|---|---|
同步 | 没有开启新线程 串行执行任务 | 没有开启新线程 串行执行任务 | 没有开启新线程 串行执行任务 |
异步 | 有开启新线程 并发执行任务 | 有开启新线程 串行执行任务 | 没有开启新线程 串行执行任务 |
网友评论