iOS多线程总结1-GCD
iOS多线程编程
写在前面,欢迎互相交流,欢迎指出错误;
基本概念
进程:计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位,是操作系统结构的基础。
线程:是进程中的一个实体,是被系统独立调度和分派的基本单位,线程自己不拥有系统资源,只拥有一点儿在运行中必不可少的资源,但它可与同属一个进程的其它线程共享进程所拥有的全部资源。
进程:就是指一个可执行的程序;
线程:指代一个独立执行的代码路径;
参考自小笨狼漫谈多线程
进程和线程:
推荐阮一峰的《进程与线程的一个简单解释》
http://www.ruanyifeng.com/blog/2013/04/processes_and_threads.html
GCD
任务和队列
任务:
任务是在线程中执行的;具体分为:
- 同步任务
dispatch_sync
; - 异步任务
dispatch_async
;
两者的区别是:同步sync
和异步async
的区别在于会不会阻塞当前线程;
队列:
队列只是负责任务的调度,而不负责任务的执行;具体分为串行队列serial queue
和并行队列concurrent queue
;
队列的特点:
- 先进先出
FIFO
,排在前面的任务最先执行; - 串行队列:任务按照顺序被调度,前一个任务不执行完毕,队列不会调度;
- 并行队列:只要有空闲的线程,队列就会调度当前任务,交给线程去执行,不需要考虑前面是不是有任务在执行、有没有执行完毕,只要有线程可以利用,队列就会调度任务。
讨论几种情况:
Tips:主队列是全局串行队列
-
主队列
main queue
, 异步任务async task
特别注意:虽然是异步任务,但是主队列并不会创建新的线程,任务会在主线程顺序执行;
dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ sleep(3); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation1 .."); }); dispatch_async(queue, ^{ sleep(2); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation2 .."); }); dispatch_async(queue, ^{ sleep(1); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation3 .."); });
2017-09-20 10:42:46.153353 iOSSyncDemo[15164:4338648] <NSThread: 0x17407cf40>{number = 1, name = main}
2017-09-20 10:42:46.153575 iOSSyncDemo[15164:4338648] operation1 ..
2017-09-20 10:42:48.155361 iOSSyncDemo[15164:4338648] <NSThread: 0x17407cf40>{number = 1, name = main}
2017-09-20 10:42:48.155553 iOSSyncDemo[15164:4338648] operation2 ..
2017-09-20 10:42:49.157272 iOSSyncDemo[15164:4338648] <NSThread: 0x17407cf40>{number = 1, name = main}
2017-09-20 10:42:49.157467 iOSSyncDemo[15164:4338648] operation3 ..
-
主队列
main queue
,同步任务sync task
,这种情况下,会彻底卡死主线程;dispatch_queue_t queue = dispatch_get_main_queue(); NSLog(@"start ..."); dispatch_sync(queue, ^{ sleep(2); NSLog(@"operation1 ..."); }); NSLog(@"finished");
2017-09-20 10:47:51.087949 iOSSyncDemo[15169:4341123] start ...
(lldb)
-
其他串行队列
other serial queue
,异步任务async task
,这种情况下,会创建新的线程,然后在新的线程上顺序执行任务;dispatch_queue_t queue = dispatch_queue_create("com.investment.SERIAL", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ sleep(3); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation1 .."); }); dispatch_async(queue, ^{ sleep(2); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation2 .."); }); dispatch_async(queue, ^{ sleep(1); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation3 .."); });
2017-09-20 10:21:01.822707 iOSSyncDemo[15148:4335599] <NSThread: 0x174260240>{number = 3, name = (null)}
2017-09-20 10:21:01.822949 iOSSyncDemo[15148:4335599] operation1 ..
2017-09-20 10:21:02.825276 iOSSyncDemo[15148:4335599] <NSThread: 0x174260240>{number = 3, name = (null)}
2017-09-20 10:21:02.825468 iOSSyncDemo[15148:4335599] operation2 ..
2017-09-20 10:21:03.831180 iOSSyncDemo[15148:4335599] <NSThread: 0x174260240>{number = 3, name = (null)}
2017-09-20 10:21:03.831372 iOSSyncDemo[15148:4335599] operation3 ..
-
其他串行队列
other serial queue
,同步步任务sync task
,这种情况下,任务会在主线程顺序执行;dispatch_queue_t queue = dispatch_queue_create("com.investment.SERIAL", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ sleep(3); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation1 .."); }); dispatch_async(queue, ^{ sleep(2); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation2 .."); }); dispatch_async(queue, ^{ sleep(1); NSLog(@"%@", [NSThread currentThread]); NSLog(@"operation3 .."); });
2017-09-20 10:57:52.900571 iOSSyncDemo[15178:4342726] <NSThread: 0x174077c80>{number = 1, name = main}
2017-09-20 10:57:52.900780 iOSSyncDemo[15178:4342726] operation1 ..
2017-09-20 10:57:54.902541 iOSSyncDemo[15178:4342726] <NSThread: 0x174077c80>{number = 1, name = main}
2017-09-20 10:57:54.902735 iOSSyncDemo[15178:4342726] operation2 ..
2017-09-20 10:57:55.904303 iOSSyncDemo[15178:4342726] <NSThread: 0x174077c80>{number = 1, name = main}
2017-09-20 10:57:55.904500 iOSSyncDemo[15178:4342726] operation3 ..
-
并发队列
concurrent queue
, 异步任务async task
,这种情况下,会为每个任务新建线程,并放到新线程上执行(注意:线程和任务一一对应)。dispatch_queue_t queue = dispatch_queue_create("com.investment.CONCURRENT", DISPATCH_QUEUE_CONCURRENT); // 并发队列 异步执行 dispatch_async(queue, ^{ sleep(3); NSLog(@"concurrentQueue async task1: %@", [NSThread currentThread]); }); dispatch_async(queue, ^{ sleep(2); NSLog(@"concurrentQueue async task2: %@", [NSThread currentThread]); }); dispatch_async(queue, ^{ sleep(1); NSLog(@"concurrentQueue async task3: %@", [NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog(@"concurrentQueue async task4: %@", [NSThread currentThread]); });
2017-09-20 11:09:27.772133 iOSSyncDemo[15186:4344364] concurrentQueue async task4: <NSThread: 0x1742673c0>{number = 3, name = (null)}
2017-09-20 11:09:28.777422 iOSSyncDemo[15186:4344365] concurrentQueue async task3: <NSThread: 0x1700785c0>{number = 4, name = (null)}
2017-09-20 11:09:29.777229 iOSSyncDemo[15186:4344366] concurrentQueue async task2: <NSThread: 0x170262440>{number = 5, name = (null)}
2017-09-20 11:09:30.777152 iOSSyncDemo[15186:4344363] concurrentQueue async task1: <NSThread: 0x170262480>{number = 6, name = (null)}
-
并发队列
concurrent queue
, 同步任务sync task
,这种情况下,所有任务会在当前线程顺序执行。dispatch_queue_t queue = dispatch_queue_create("com.investment.CONCURRENT", DISPATCH_QUEUE_CONCURRENT); // 并发队列 异步执行 dispatch_sync(queue, ^{ sleep(3); NSLog(@"concurrentQueue async task1: %@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ sleep(2); NSLog(@"concurrentQueue async task2: %@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ sleep(1); NSLog(@"concurrentQueue async task3: %@", [NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog(@"concurrentQueue async task4: %@", [NSThread currentThread]); });
2017-09-20 11:11:57.385978 iOSSyncDemo[15189:4344846] concurrentQueue async task1: <NSThread: 0x174076300>{number = 1, name = main}
2017-09-20 11:11:59.387875 iOSSyncDemo[15189:4344846] concurrentQueue async task2: <NSThread: 0x174076300>{number = 1, name = main}
2017-09-20 11:12:00.389544 iOSSyncDemo[15189:4344846] concurrentQueue async task3: <NSThread: 0x174076300>{number = 1, name = main}
2017-09-20 11:12:00.389850 iOSSyncDemo[15189:4344846] concurrentQueue async task4: <NSThread: 0x174076300>{number = 1, name = main}
主队列:这是一个特殊的串行队列
。所有的UI操作都在这个队列上;
dispatch_get_main_queue()
全局并行队列:这是系统提供的一个并发队列;
dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)
dispatch_group
-
dispatch_group_enter
和dispatch_group_leave
dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_async(group, queue, ^{ dispatch_async(queue, ^{ sleep(2); NSLog(@"group task1 executing..."); }); NSLog(@"group task1 finished"); }); dispatch_group_async(group, queue, ^{ dispatch_async(queue, ^{ sleep(2); NSLog(@"group task2 executing..."); }); NSLog(@"group task2 finished"); }); dispatch_group_async(group, queue, ^{ dispatch_async(queue, ^{ sleep(2); NSLog(@"group task3 executing..."); }); NSLog(@"group task3 finished"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"all group task finished"); });
2017-09-20 11:20:48.762150 iOSSyncDemo[15197:4347820] group task1 finished
2017-09-20 11:20:48.762258 iOSSyncDemo[15197:4347820] group task2 finished
2017-09-20 11:20:48.762629 iOSSyncDemo[15197:4347820] group task3 finished
2017-09-20 11:20:48.770034 iOSSyncDemo[15197:4347795] all group task finished
2017-09-20 11:20:50.768016 iOSSyncDemo[15197:4347817] group task1 executing...
2017-09-20 11:20:50.769131 iOSSyncDemo[15197:4347820] group task2 executing...
2017-09-20 11:20:50.769255 iOSSyncDemo[15197:4347818] group task3 executing...
通过log我们可以发现,在子线程的任务还没有完成的情况下;组任务就直接完成,这显然不是我们想要的结果;现在改进一下:
dispatch_group_t group = dispatch_group_create();
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"group task1 executing...");
sleep(3);
NSLog(@"group task1 finished");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"group task2 executing...");
sleep(2);
NSLog(@"group task2 finished");
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async(queue, ^{
NSLog(@"group task3 executing...");
sleep(1);
NSLog(@"group task3 finished");
dispatch_group_leave(group);
});
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"all group task finished");
});
2017-09-20 11:30:32.554690 iOSSyncDemo[15212:4350152] group task1 executing...
2017-09-20 11:30:32.554844 iOSSyncDemo[15212:4350156] group task2 executing...
2017-09-20 11:30:32.555487 iOSSyncDemo[15212:4350154] group task3 executing...
2017-09-20 11:30:33.560874 iOSSyncDemo[15212:4350154] group task3 finished
2017-09-20 11:30:34.560076 iOSSyncDemo[15212:4350156] group task2 finished
2017-09-20 11:30:35.560065 iOSSyncDemo[15212:4350152] group task1 finished
2017-09-20 11:30:35.560380 iOSSyncDemo[15212:4350130] all group task finished
这个时候就达到我们想要的结果了。
-
常规用法,这种用法在开发中可以应用的场景不多,更多的情况是上面那种并发队列多异步任务嵌套的情况。
dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_async(group, queue, ^{ sleep(2); NSLog(@"group task1 finished"); }); dispatch_group_async(group, queue, ^{ sleep(2); NSLog(@"group task2 finished"); }); dispatch_group_async(group, queue, ^{ sleep(2); NSLog(@"group task3 finished"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"all group task finished"); }); dispatch_group_wait(group, DISPATCH_TIME_FOREVER); NSLog(@"after wait ..");
打印结果:
2017-09-19 18:15:48.088 iOSSyncDemo[17731:1209181] group task3 finished
2017-09-19 18:15:48.088 iOSSyncDemo[17731:1209183] group task1 finished
2017-09-19 18:15:48.088 iOSSyncDemo[17731:1209180] group task2 finished
2017-09-19 18:15:48.089 iOSSyncDemo[15192:4346855] after wait ..
2017-09-19 18:15:48.089 iOSSyncDemo[17731:1208970] all group task finished
dispatch_once
多用于单例
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
//单例代码
});
dispatch_after
注意:dispatch_after是延迟提交,不是延迟运行;
作用是:将一个Block在特定的延时以后,加入到指定的队列中,并执行;
dispatch_queue_t queue = dispatch_queue_create("com.investment.concurrent", DISPATCH_QUEUE_CONCURRENT);
NSLog(@"Begin add operation...");
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"operation1 done...");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:4];
NSLog(@"operation2 done...");
});
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), queue, ^{
NSLog(@"after operation");
});
2017-09-20 09:58:06.633353 iOSSyncDemo[15132:4331075] Begin add operation...
2017-09-20 09:58:08.818947 iOSSyncDemo[15132:4331094] after operation
2017-09-20 09:58:10.639082 iOSSyncDemo[15132:4331095] operation1 done...
2017-09-20 09:58:10.639383 iOSSyncDemo[15132:4331097] operation2 done...
dispatch_suspend
暂停队列,但是这里的“暂停”,并不能保证可以立即停止队列上正在运行的block;dispatch_suspend
并不会立即暂停正在运行的block,而是在当前block执行完成后,暂停后续的block执行。
dispatch_queue_t queue = dispatch_queue_create("com.investment.SERIAL", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"operation 1 executing...");
});
dispatch_async(queue, ^{
[NSThread sleepForTimeInterval:5];
NSLog(@"operation 2 executing...");
});
//延时一秒
NSLog(@"sleep 1 second...");
[NSThread sleepForTimeInterval:1];
//挂起队列
NSLog(@"suspend...");
dispatch_suspend(queue);
//延时10秒
NSLog(@"sleep 10 second...");
[NSThread sleepForTimeInterval:10];
//恢复队列
NSLog(@"resume...");
dispatch_resume(queue);
2017-09-20 12:02:56.875441 iOSSyncDemo[15232:4356001] sleep 1 second...
2017-09-20 12:02:57.877435 iOSSyncDemo[15232:4356001] suspend...
2017-09-20 12:02:57.877667 iOSSyncDemo[15232:4356001] sleep 10 second...
2017-09-20 12:03:01.880958 iOSSyncDemo[15232:4356027] operation 1 executing...
2017-09-20 12:03:07.878927 iOSSyncDemo[15232:4356001] resume...
2017-09-20 12:03:12.890141 iOSSyncDemo[15232:4356024] operation 2 executing...
dispatch_apply
这个方法是同步的
! 会阻塞当前线程
;
dispatch_queue_t queue = dispatch_queue_create("com.investment.CONCURRENT", DISPATCH_QUEUE_CONCURRENT);
dispatch_apply(3, queue, ^(size_t i) {
sleep(1);
NSLog(@"%@", [NSThread currentThread]);
NSLog(@"loop: %zu", i);
});
NSLog(@"after apply operation...");
2017-09-20 09:51:09.230843 iOSSyncDemo[15125:4329829] <NSThread: 0x17406fac0>{number = 1, name = main}
2017-09-20 09:51:09.231062 iOSSyncDemo[15125:4329829] loop: 0
2017-09-20 09:51:09.233557 iOSSyncDemo[15125:4329856] <NSThread: 0x17007dbc0>{number = 3, name = (null)}
2017-09-20 09:51:09.233747 iOSSyncDemo[15125:4329856] loop: 1
2017-09-20 09:51:10.232754 iOSSyncDemo[15125:4329829] <NSThread: 0x17406fac0>{number = 1, name = main}
2017-09-20 09:51:10.232955 iOSSyncDemo[15125:4329829] loop: 2
2017-09-20 09:51:10.233043 iOSSyncDemo[15125:4329829] after apply operation...
dispatch_semaphore_t
用于线程间同步,比如下面这个生产者消费者模型。
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
dispatch_queue_t queue = dispatch_queue_create("com.investment.concurrent", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
for (int i = 0; i < 3; i ++) {
sleep(2);
NSLog(@"create one product");
dispatch_semaphore_signal(sem);
}
});
dispatch_async(queue, ^{
for (int i = 0; i < 100; i ++) {
dispatch_semaphore_wait(sem, DISPATCH_TIME_FOREVER);
NSLog(@"consume one product");
}
});
2017-09-20 12:25:19.951850 iOSSyncDemo[15250:4360783] create one product
2017-09-20 12:25:19.952206 iOSSyncDemo[15250:4360780] consume one product
2017-09-20 12:25:21.955972 iOSSyncDemo[15250:4360783] create one product
2017-09-20 12:25:21.956282 iOSSyncDemo[15250:4360780] consume one product
2017-09-20 12:25:23.961401 iOSSyncDemo[15250:4360783] create one product
2017-09-20 12:25:23.961720 iOSSyncDemo[15250:4360780] consume one product
dispatch_set_target_queue
设置队列的优先级,将参数一的队列的优先级设置成和参数二的优先级一样。
dispatch_set_target_queue(queue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));