串行(Serial)
任务只能一个个按顺序执行,只能等到上一个任务执行完毕,才能执行当前的任务;
并发 (Concurrent):
任务不按顺序执行,不用等待上一个任务执行完,多个任务是同时执行的;在异步情况下可开启多个线程执行任务;
同步(Synchronous):
异步 (Asynchronous):
队列 (Dispatch Queue)
苹果为我们提供了串行队列,并行队列,全局队列,主队列!
dispatch_queue_t queue = dispatch_queue_create("com.mjz.cn", DISPATCH_QUEUE_SERIAL);
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
dispatch_queue_t queue = dispatch_queue_create(0, 0);
dispatch_queue_t queue = dispatch_queue_create("com.mjz.cn", DISPATCH_QUEUE_CONCURRENT);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0);
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_queue_t queue = dispatch_queue_create(NULL, NULL);
for (NSInteger index=0; index<20; index++) {
dispatch_sync(queue, ^{
NSLog(@"%ld<-->%@-->%f", index, [NSThread currentThread], [NSThread currentThread].threadPriority);
});
}
结论:任务按顺序执行,下一个任务只有等待上个任务执行完才能执行!
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
for (NSInteger index=0; index<20; index++) {
dispatch_sync(queue, ^{
NSLog(@"%ld<-->%@-->%f", index, [NSThread currentThread], [NSThread currentThread].threadPriority);
});
}
结论:虽然是并行队列,但是dispatch_sync不会开启线程,所以任务还是按顺序执行,跟上面一样的结果!
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
for (NSInteger index=0; index<20; index++) {
dispatch_async(queue, ^{
NSLog(@"%ld<-->%@", index, [NSThread currentThread]);
});
}
结论:串行队列任务按顺序执行,但dispatch_async会开启一个新的线程执行!
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
for (NSInteger index=0; index<20; index++) {
dispatch_async(queue, ^{
NSLog(@"%ld<-->%@", index, [NSThread currentThread]);
});
}
结论:并行队列任务同时执行,加上dispatch_async会开启新线程,所以此时会生成多个线程同时执行任务!
在串行队列情形下,任务都是按顺序逐一执行;但是到了并行队列的状态下,队列要实现某个任务等到其它队列执行完后才执行就有点难了,这时候我们需要引入了Dispatch Group!
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_group_async(group, queue, ^{
NSLog(@"11 %@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"22 %@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"44 %@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"55 %@", [NSThread currentThread]);
});
dispatch_group_async(group, queue, ^{
NSLog(@"66 %@", [NSThread currentThread]);
});
NSLog(@"-------------------------------------");
dispatch_group_notify(group, queue, ^{
NSLog(@"33 %@", [NSThread currentThread]);
});
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"11");
});
dispatch_async(queue, ^{
NSLog(@"22");
});
dispatch_async(queue, ^{
NSLog(@"33");
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"66");
dispatch_group_leave(group);
});
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
dispatch_block_t block = dispatch_block_create(0, ^{
NSLog(@"mjz");
});
dispatch_async(queue, block);
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_SERIAL);
dispatch_block_t block = dispatch_block_create(0, ^{
sleep(5.0);
NSLog(@"mjz");
});
dispatch_async(queue, block);
long result = dispatch_block_wait(block, dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC*5.0));
if (result==0) {
NSLog(@"wait finish!!");
} else {
NSLog(@"wait error!!");
}
dispatch_queue_t queue = dispatch_queue_create("com.mjz.www", DISPATCH_QUEUE_CONCURRENT);
dispatch_block_t block = dispatch_block_create(0, ^{
NSLog(@"block start");
[NSThread sleepForTimeInterval:1];
NSLog(@"block1 end");
});
dispatch_block_t block2 = dispatch_block_create(0, ^{
NSLog(@"block2 finish");
});
dispatch_async(queue, block);
dispatch_async(queue, block2);
dispatch_block_cancel(block2);
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(6, queue, ^(size_t i) {
NSLog(@"index: %ld",i);
});
NSLog(@"end!!!");
dispatch_queue_t queue = dispatch_queue_create(NULL, DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:6];
NSLog(@"11");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:2];
NSLog(@"22");
});
dispatch_barrier_async(queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"33");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:1];
NSLog(@"44");
});