创建队列
创建串行队列
dispatch_queue_t queue2 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_SERIAL);
创建并行队列
dispatch_queue_t queue2 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_CONCURRENT);
获取全局队列
dispatch_queue_t queue2 = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
系统提供dispatch方法
创建异步任务,开启新线程,不阻塞当前线程
dispatch_async(queue3, ^{
});
创建同步任务,不开启新线程,阻塞当前线程
dispatch_sync(queue1, ^{
});
主线程执行
dispatch_async(dispatch_get_main_queue(), ^{
});
一次性执行
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
});
延迟 2 秒执行
double delayInSeconds = 2.0;
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
});
创建队列组
dispatch_group_t group = dispatch_group_create();
例子
eg.1
//创建串行队列
dispatch_queue_t queue2 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_SERIAL);
//创建异步任务,开启新线程,不阻塞当前线程
dispatch_async(queue2, ^{
for (int i=0; i<=100; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"aaaaa");
//创建异步任务,开启新线程,不阻塞当前线程
dispatch_async(queue2, ^{
for (int i=101; i<=200; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"bbbbb");
打印结果如下,由打印结果可以看出dispatch_async没有阻塞当前线程,会另开一个线程。而串行队列queue2则会保证,在另一个线程中的任务顺序执行。
2016-09-19 10:11:21.090 ThreadTest[3142:78551] _______0
2016-09-19 10:11:21.089 ThreadTest[3142:78508] aaaaa
2016-09-19 10:11:21.091 ThreadTest[3142:78508] bbbbb
2016-09-19 10:11:21.091 ThreadTest[3142:78551] _______1
2016-09-19 10:11:21.092 ThreadTest[3142:78551] _______2
2016-09-19 10:11:21.093 ThreadTest[3142:78551] _______3
2016-09-19 10:11:21.094 ThreadTest[3142:78551] _______4
2016-09-19 10:11:21.094 ThreadTest[3142:78551] _______5
2016-09-19 10:11:21.094 ThreadTest[3142:78551] _______6
2016-09-19 10:11:21.095 ThreadTest[3142:78551] _______7
2016-09-19 10:11:21.095 ThreadTest[3142:78551] _______8
2016-09-19 10:11:21.095 ThreadTest[3142:78551] _______9
2016-09-19 10:11:21.096 ThreadTest[3142:78551] _______10
2016-09-19 10:11:21.096 ThreadTest[3142:78551] _______11
2016-09-19 10:11:21.096 ThreadTest[3142:78551] _______12
2016-09-19 10:11:21.096 ThreadTest[3142:78551] _______13
2016-09-19 10:11:21.097 ThreadTest[3142:78551] _______14
2016-09-19 10:11:21.097 ThreadTest[3142:78551] _______15
2016-09-19 10:11:21.097 ThreadTest[3142:78551] _______16
2016-09-19 10:11:21.097 ThreadTest[3142:78551] _______17
2016-09-19 10:11:21.098 ThreadTest[3142:78551] _______18
2016-09-19 10:11:21.098 ThreadTest[3142:78551] _______19
2016-09-19 10:11:21.099 ThreadTest[3142:78551] _______20
eg.2
//创建并行队列
dispatch_queue_t queue3 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_CONCURRENT);
//创建异步任务,开启新线程,不阻塞当前线程
dispatch_async(queue3, ^{
for (int i=0; i<=10; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"aaaaa");
//创建异步任务,开启新线程,不阻塞当前线程
dispatch_async(queue3, ^{
for (int i=11; i<=20; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"bbbbb");
由下方打印结果可以看出dispatch_async会另开一个线程,不阻塞当前线程。而并行队列queue3则会导致另开线程中的任务并发执行。
2016-09-19 10:18:24.252 ThreadTest[3199:82172] aaaaa
2016-09-19 10:18:24.252 ThreadTest[3199:82208] _______0
2016-09-19 10:18:24.254 ThreadTest[3199:82172] bbbbb
2016-09-19 10:18:24.254 ThreadTest[3199:82208] _______1
2016-09-19 10:18:24.254 ThreadTest[3199:82210] _______11
2016-09-19 10:18:24.254 ThreadTest[3199:82208] _______2
2016-09-19 10:18:24.255 ThreadTest[3199:82210] _______12
2016-09-19 10:18:24.255 ThreadTest[3199:82208] _______3
2016-09-19 10:18:24.255 ThreadTest[3199:82210] _______13
2016-09-19 10:18:24.255 ThreadTest[3199:82208] _______4
2016-09-19 10:18:24.255 ThreadTest[3199:82210] _______14
2016-09-19 10:18:24.256 ThreadTest[3199:82208] _______5
2016-09-19 10:18:24.256 ThreadTest[3199:82210] _______15
2016-09-19 10:18:24.256 ThreadTest[3199:82208] _______6
2016-09-19 10:18:24.256 ThreadTest[3199:82210] _______16
2016-09-19 10:18:24.256 ThreadTest[3199:82208] _______7
2016-09-19 10:18:24.256 ThreadTest[3199:82210] _______17
2016-09-19 10:18:24.257 ThreadTest[3199:82208] _______8
2016-09-19 10:18:24.257 ThreadTest[3199:82210] _______18
2016-09-19 10:18:24.257 ThreadTest[3199:82208] _______9
2016-09-19 10:18:24.258 ThreadTest[3199:82210] _______19
2016-09-19 10:18:24.258 ThreadTest[3199:82208] _______10
2016-09-19 10:18:24.258 ThreadTest[3199:82210] _______20
eg.3
//创建并行队列
dispatch_queue_t queue3 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_CONCURRENT);
//创建同步任务,不开启新线程,阻塞当前线程
dispatch_sync(queue3, ^{
for (int i=0; i<=10; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"aaaaa");
//创建同步任务,不开启新线程,阻塞当前线程
dispatch_sync(queue3, ^{
for (int i=11; i<=20; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"bbbbb");
//创建串行队列
dispatch_queue_t queue2 =
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_SERIAL);
//创建同步任务
dispatch_sync(queue2, ^{
for (int i=0; i<=10; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"aaaaa");
//创建同步任务
dispatch_sync(queue2, ^{
for (int i=11; i<=20; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"bbbbb");
上面两段代码的打印结果相同,如下所示。由此可以看出不管队列是串行和并行dispatch_sync都会阻塞当前线程。是否阻塞当前线程和队列无关,仅和任务有关。
2016-09-19 10:23:05.241 ThreadTest[3283:85234] _______0
2016-09-19 10:23:05.242 ThreadTest[3283:85234] _______1
2016-09-19 10:23:05.242 ThreadTest[3283:85234] _______2
2016-09-19 10:23:05.242 ThreadTest[3283:85234] _______3
2016-09-19 10:23:05.243 ThreadTest[3283:85234] _______4
2016-09-19 10:23:05.243 ThreadTest[3283:85234] _______5
2016-09-19 10:23:05.243 ThreadTest[3283:85234] _______6
2016-09-19 10:23:05.243 ThreadTest[3283:85234] _______7
2016-09-19 10:23:05.243 ThreadTest[3283:85234] _______8
2016-09-19 10:23:05.244 ThreadTest[3283:85234] _______9
2016-09-19 10:23:05.244 ThreadTest[3283:85234] _______10
2016-09-19 10:23:05.244 ThreadTest[3283:85234] aaaaa
2016-09-19 10:23:05.244 ThreadTest[3283:85234] _______11
2016-09-19 10:23:05.244 ThreadTest[3283:85234] _______12
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______13
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______14
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______15
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______16
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______17
2016-09-19 10:23:05.245 ThreadTest[3283:85234] _______18
2016-09-19 10:23:05.246 ThreadTest[3283:85234] _______19
2016-09-19 10:23:05.246 ThreadTest[3283:85234] _______20
2016-09-19 10:23:05.246 ThreadTest[3283:85234] bobby
eg.4
//创建队列组
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue3 =
//创建并行队列
dispatch_queue_create("com.quanya.queue", DISPATCH_QUEUE_CONCURRENT);
//创建异步任务,不阻塞当前线程
dispatch_group_async(group,queue3, ^{
for (int i=0; i<=10; i++) {
NSLog(@"_______%d",i);
}
});
NSLog(@"aaaaa");
//创建异步任务,不阻塞当前线程
dispatch_group_async(group,queue3, ^{
for (int i=11; i<=20; i++) {
NSLog(@"_______%d",i);
}
});
//4.都完成后会自动通知
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"_______完成");
});
NSLog(@"bbbbb");
以上是队列组dispatch_group_t的使用,在所有任务执行完时会调用dispatch_group_notify
2016-09-19 10:29:18.989 ThreadTest[3377:88634] aaaaa
2016-09-19 10:29:18.989 ThreadTest[3377:88677] _______0
2016-09-19 10:29:18.990 ThreadTest[3377:88677] _______1
2016-09-19 10:29:18.991 ThreadTest[3377:88634] bbbbb
2016-09-19 10:29:18.991 ThreadTest[3377:88679] _______11
2016-09-19 10:29:18.991 ThreadTest[3377:88677] _______2
2016-09-19 10:29:18.991 ThreadTest[3377:88679] _______12
2016-09-19 10:29:18.991 ThreadTest[3377:88677] _______3
2016-09-19 10:29:18.992 ThreadTest[3377:88679] _______13
2016-09-19 10:29:18.992 ThreadTest[3377:88677] _______4
2016-09-19 10:29:18.992 ThreadTest[3377:88679] _______14
2016-09-19 10:29:18.993 ThreadTest[3377:88677] _______5
2016-09-19 10:29:18.993 ThreadTest[3377:88679] _______15
2016-09-19 10:29:18.993 ThreadTest[3377:88677] _______6
2016-09-19 10:29:18.993 ThreadTest[3377:88679] _______16
2016-09-19 10:29:18.993 ThreadTest[3377:88677] _______7
2016-09-19 10:29:18.993 ThreadTest[3377:88679] _______17
2016-09-19 10:29:18.993 ThreadTest[3377:88677] _______8
2016-09-19 10:29:18.994 ThreadTest[3377:88679] _______18
2016-09-19 10:29:18.995 ThreadTest[3377:88677] _______9
2016-09-19 10:29:18.995 ThreadTest[3377:88679] _______19
2016-09-19 10:29:18.995 ThreadTest[3377:88677] _______10
2016-09-19 10:29:18.995 ThreadTest[3377:88679] _______20
2016-09-19 10:29:19.089 ThreadTest[3377:88634] _______完成
划重点
dispatch_queue_t仅能控制任务执行的方式,并行或串行。同步和异步任务决定是否开启新线程去执行。在主线程中同步和异步任务都会阻塞主线。