之前去面試時被考官給的一道多線程任務(wù)執(zhí)行順序問題給問到了,之前對這塊一直學(xué)得不清不楚昼丑,現(xiàn)在剛好有空測試一下。下面先上代碼與執(zhí)行結(jié)果:
????dispatch_queue_t serialQueue = dispatch_queue_create("串行隊列", DISPATCH_QUEUE_SERIAL);
? ? dispatch_queue_tconcurrentQueue =dispatch_queue_create("并行隊列",DISPATCH_QUEUE_CONCURRENT);
? ? dispatch_queue_t mainQueue = dispatch_get_main_queue();
? ? dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
? ? //1
? ? for (inti =0; i <10; i++) {
? ? ? ? dispatch_sync(serialQueue, ^{
? ? ? ? ? ? NSLog(@"任務(wù)執(zhí)行中夸赫。菩帝。。-%d, mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 16:45:56.881958+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中茬腿。呼奢。。-0, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882092+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中切平。握础。。-1, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882174+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中悴品。禀综。。-2, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882236+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中苔严。定枷。。-3, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882293+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中届氢。欠窒。。-4, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882348+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中退子。贱迟。姐扮。-5, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882469+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中。衣吠。茶敏。-6, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882551+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中。缚俏。惊搏。-7, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882617+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中。忧换。恬惯。-8, mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:45:56.882677+0800 ttttt[11609:14893507] 任務(wù)執(zhí)行中。亚茬。酪耳。-9, mainThread:{number = 1, name = main}
?? ? */
? ? //運行結(jié)果是,同步任務(wù)在串行隊列中刹缝,在主線程下串行執(zhí)行
? ? //2
? ? for (inti=0; i<5; i++) {
? ? ? ? dispatch_sync(concurrentQueue, ^{
? ? ? ? ? ? NSLog(@"同步任務(wù)在并行隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 同步任務(wù)在并行隊列執(zhí)行中....-0
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:50:37.411207+0800 ttttt[11688:14904522] 同步任務(wù)在并行隊列執(zhí)行中....-1
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:50:37.411339+0800 ttttt[11688:14904522] 同步任務(wù)在并行隊列執(zhí)行中....-2
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:50:37.411417+0800 ttttt[11688:14904522] 同步任務(wù)在并行隊列執(zhí)行中....-3
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 16:50:37.411527+0800 ttttt[11688:14904522] 同步任務(wù)在并行隊列執(zhí)行中....-4
?? ? mainThread:{number = 1, name = main}
?? ? */
? ? //結(jié)果碗暗,同步任務(wù)在并行隊列中,在主線程下串行執(zhí)行
? ? //3
//? ? for(int i=0;i<10;i++){
//? ? ? ? ? ? dispatch_sync(mainQueue, ^{
//? ? ? ? ? ? ? ? NSLog(@"任務(wù)執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
//? ? ? ? ? ? });
//? ? ? ? }
? ? //結(jié)果梢夯,同步任務(wù)在主隊列中言疗,鎖死
? ? //4
? ? for (inti=0; i<5; i++) {
? ? ? ? dispatch_sync(globalQueue, ^{
? ? ? ? ? ? NSLog(@"同步任務(wù)在全局隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 17:03:36.779303+0800 ttttt[11929:14946556] 同步任務(wù)在全局隊列執(zhí)行中....-0
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:03:36.779507+0800 ttttt[11929:14946556] 同步任務(wù)在全局隊列執(zhí)行中....-1
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:03:36.779630+0800 ttttt[11929:14946556] 同步任務(wù)在全局隊列執(zhí)行中....-2
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:03:36.779729+0800 ttttt[11929:14946556] 同步任務(wù)在全局隊列執(zhí)行中....-3
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:03:36.779884+0800 ttttt[11929:14946556] 同步任務(wù)在全局隊列執(zhí)行中....-4
?? ? mainThread:{number = 1, name = main}
?? ? */
? ? //結(jié)果,同步任務(wù)在全局隊列中颂砸,在主線程下串行執(zhí)行
? ? //5
? ? for (inti=0; i<10; i++) {
? ? ? ? dispatch_async(serialQueue, ^{
? ? ? ? ? ? NSLog(@"異步任務(wù)在串行隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 17:06:58.915475+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-0
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.915556+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-1
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.915690+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-2
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.916051+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-3
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.916435+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-4
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.916613+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-5
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.917091+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-6
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.917218+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-7
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.917451+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-8
?? ? mainThread:{number = 3, name = (null)}
?? ? 2018-08-22 17:06:58.917571+0800 ttttt[11993:14954479] 異步任務(wù)在串行隊列執(zhí)行中....-9
?? ? mainThread:{number = 3, name = (null)}
?? ? */
? ? //結(jié)果噪奄,異步任務(wù)在串行隊列中,是在一個子線程中串行執(zhí)行
? ? //6
? ? for (inti=0; i<5; i++) {
? ? ? ? dispatch_async(concurrentQueue, ^{
?? ? ? ? ? NSLog(@"異步任務(wù)在并行隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 17:11:37.539218+0800 ttttt[12080:14967734] 異步任務(wù)在并行隊列執(zhí)行中....-1
?? ? mainThread:{number = 5, name = (null)}
?? ? 2018-08-22 17:11:37.539219+0800 ttttt[12080:14967733] 異步任務(wù)在并行隊列執(zhí)行中....-0
?? ? mainThread:{number = 4, name = (null)}
?? ? 2018-08-22 17:11:37.539249+0800 ttttt[12080:14967736] 異步任務(wù)在并行隊列執(zhí)行中....-2
?? ? mainThread:{number = 6, name = (null)}
?? ? 2018-08-22 17:11:37.539522+0800 ttttt[12080:14967744] 異步任務(wù)在并行隊列執(zhí)行中....-3
?? ? mainThread:{number = 7, name = (null)}
?? ? 2018-08-22 17:11:37.539566+0800 ttttt[12080:14967745] 異步任務(wù)在并行隊列執(zhí)行中....-4
?? ? mainThread:{number = 8, name = (null)}
?? ? */
? ? //結(jié)果人乓,異步任務(wù)在并行隊列中勤篮,是在多個子線程中并行執(zhí)行,順序不確定
? ? //7
? ? for (inti=0; i<7; i++) {
? ? ? ? dispatch_async(mainQueue, ^{
? ? ? ? ? ? NSLog(@"異步任務(wù)在主隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 17:18:08.413412+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-1
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:18:08.413520+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-2
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:18:08.413746+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-3
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:18:08.413923+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-4
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:18:08.414136+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-5
?? ? mainThread:{number = 1, name = main}
?? ? 2018-08-22 17:18:08.414205+0800 ttttt[12189:14986737] 異步任務(wù)在主隊列執(zhí)行中....-6
?? ? mainThread:{number = 1, name = main}
?? ? */
? ? //結(jié)果色罚,異步任務(wù)在主隊列中執(zhí)行碰缔,是在主線程中串行執(zhí)行
? ? //8
? ? for (inti=0; i<6; i++) {
? ? ? ? dispatch_async(globalQueue, ^{
? ? ? ? ? ? NSLog(@"異步任務(wù)在全局隊列執(zhí)行中....-%d\n mainThread:%@",i,[NSThread currentThread]);
? ? ? ? });
? ? }
? ? /*
?? ? 2018-08-22 17:22:04.895288+0800 ttttt[12266:14996623] 異步任務(wù)在全局隊列執(zhí)行中....-0
?? ? mainThread:{number = 9, name = (null)}
?? ? 2018-08-22 17:22:04.895322+0800 ttttt[12266:14996624] 異步任務(wù)在全局隊列執(zhí)行中....-1
?? ? mainThread:{number = 10, name = (null)}
?? ? 2018-08-22 17:22:04.895344+0800 ttttt[12266:14996625] 異步任務(wù)在全局隊列執(zhí)行中....-2
?? ? mainThread:{number = 11, name = (null)}
?? ? 2018-08-22 17:22:04.895397+0800 ttttt[12266:14996626] 異步任務(wù)在全局隊列執(zhí)行中....-3
?? ? mainThread:{number = 12, name = (null)}
?? ? 2018-08-22 17:22:04.895470+0800 ttttt[12266:14996629] 異步任務(wù)在全局隊列執(zhí)行中....-5
?? ? mainThread:{number = 14, name = (null)}
?? ? 2018-08-22 17:22:04.895475+0800 ttttt[12266:14996628] 異步任務(wù)在全局隊列執(zhí)行中....-4
?? ? mainThread:{number = 13, name = (null)}
?? ? */
? ? //結(jié)果,異步任務(wù)在全局隊列中執(zhí)行保屯,是在多個子線程中并行執(zhí)行
總結(jié):
1手负、同步任務(wù)在自定義串行隊列、自定義并行隊列以及全局隊列中執(zhí)行時姑尺,都是在當(dāng)前線程中串行執(zhí)行(我的測試代碼中是在主線程中提交任務(wù)竟终,所以結(jié)果顯示是在主線程中串行執(zhí)行);但是同步任務(wù)在主隊列中執(zhí)行會造成死鎖切蟋,死鎖具體原因隨后在開篇記錄统捶。
2、異步任務(wù)在自定義串行隊列、自定義并行隊列以及全局隊列中執(zhí)行時喘鸟,都是會開辟子線程執(zhí)行匆绣,串行隊列會開辟一條子線程串行執(zhí)行,并行隊列和全局隊列中會開辟多條子線程并行執(zhí)行什黑;但是異步任務(wù)在主隊列中執(zhí)行時崎淳,不會開辟子線程,而是會在主隊列所屬的主線程中串行執(zhí)行愕把。