iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合
生活随笔
收集整理的這篇文章主要介紹了
iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
?
通過對前面兩偏線程理解的總結,自己對線程的理解也逐漸加深,梳理的清晰起來……
?
通常在使用線程 的時候,都是要用到 執行對列,執行方式,執行任務,
現在開始新一輪的深入
?
3. 1. 1? 同步 + 串行
- (void)syncSerialQueue{dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);NSLog(@" 同步 + 串行 start:::%@ ",[NSThread currentThread]);dispatch_sync(queue, ^{ // 添加任務 1for (int i = 0; i < 3; i++) {NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]);}});dispatch_sync(queue, ^{ // 添加任務 2for (int i = 10; i < 13; i++) {NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]);}});NSLog(@"同步 + 串行 end :::%@",[NSThread currentThread]); }?執行結果:::
2017-12-21 09:10:16.725075+0800 DeadThread[10455:3327379] 同步 + 串行 start:::<NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725222+0800 DeadThread[10455:3327379] 同步 + 串行 index 0 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725339+0800 DeadThread[10455:3327379] 同步 + 串行 index 1 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725530+0800 DeadThread[10455:3327379] 同步 + 串行 index 2 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725615+0800 DeadThread[10455:3327379] 同步 + 串行 index 10 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725765+0800 DeadThread[10455:3327379] 同步 + 串行 index 11 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725821+0800 DeadThread[10455:3327379] 同步 + 串行 index 12 ::: <NSThread: 0x60800006b4c0>{number = 1, name = main} 2017-12-21 09:10:16.725870+0800 DeadThread[10455:3327379] 同步 + 串行 end :::<NSThread: 0x60800006b4c0>{number = 1, name = main}?總結結果:::
1.?? 同步 : 在當前線程執行,不開啟新的線程,任務順序執行
2.? 串行 :添加的任務 順序排列,順序執行
圖示::::
?
3.1.2? 同步 +? 并行
- (void)syncConcurrentQueue{dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);NSLog(@"同步 + 并行 start:::%@ ",[NSThread currentThread]);dispatch_sync(queue, ^{for (int i = 0; i < 3; i++) {NSLog(@"同步 + 并行 index %d ::: %@",i,[NSThread currentThread]);}});dispatch_sync(queue, ^{for (int i = 10; i < 13; i++) {NSLog(@"同步 + 并行 index %d ::: %@",i,[NSThread currentThread]);}});NSLog(@"同步 + 并行 end :::%@",[NSThread currentThread]); }執行結果:::
2017-12-21 09:37:05.376797+0800 DeadThread[10595:3430843] 同步 + 并行 start:::<NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.376915+0800 DeadThread[10595:3430843] 同步 + 并行 index 0 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377000+0800 DeadThread[10595:3430843] 同步 + 并行 index 1 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377101+0800 DeadThread[10595:3430843] 同步 + 并行 index 2 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377262+0800 DeadThread[10595:3430843] 同步 + 并行 index 10 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377372+0800 DeadThread[10595:3430843] 同步 + 并行 index 11 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377440+0800 DeadThread[10595:3430843] 同步 + 并行 index 12 ::: <NSThread: 0x6040000759c0>{number = 1, name = main} 2017-12-21 09:37:05.377514+0800 DeadThread[10595:3430843] 同步 + 并行 end :::<NSThread: 0x6040000759c0>{number = 1, name = main}?總結結果:::
1.?? 同步 : 在當前線程執行,不開啟新的線程,任務順序執行
2.? 并行 :添加的任務 不是順序排列
圖示::::
?
3.2.1 異步 + 串行
- (void)asyncSerialQueue{dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL);NSLog(@"異步 + 串行 start:::%@ ",[NSThread currentThread]);dispatch_async(queue, ^{for (int i = 0; i < 3; i++) {NSLog(@"異步 + 串行 index %d ::: %@",i,[NSThread currentThread]);}});dispatch_async(queue, ^{for (int i = 10; i < 13; i++) {NSLog(@"異步 + 串行 index %d ::: %@",i,[NSThread currentThread]);}});NSLog(@"異步 + 串行 end :::%@",[NSThread currentThread]); }?執行結果::::
2017-12-21 09:43:04.903888+0800 DeadThread[10647:3468013] 異步 + 串行 start:::<NSThread: 0x60c0000654c0>{number = 1, name = main} 2017-12-21 09:43:04.904032+0800 DeadThread[10647:3468013] 異步 + 串行 end :::<NSThread: 0x60c0000654c0>{number = 1, name = main} 2017-12-21 09:43:04.904056+0800 DeadThread[10647:3468266] 異步 + 串行 index 0 ::: <NSThread: 0x604000074580>{number = 3, name = (null)} 2017-12-21 09:43:04.904127+0800 DeadThread[10647:3468266] 異步 + 串行 index 1 ::: <NSThread: 0x604000074580>{number = 3, name = (null)} 2017-12-21 09:43:04.904180+0800 DeadThread[10647:3468266] 異步 + 串行 index 2 ::: <NSThread: 0x604000074580>{number = 3, name = (null)} 2017-12-21 09:43:04.904370+0800 DeadThread[10647:3468266] 異步 + 串行 index 10 ::: <NSThread: 0x604000074580>{number = 3, name = (null)} 2017-12-21 09:43:04.904534+0800 DeadThread[10647:3468266] 異步 + 串行 index 11 ::: <NSThread: 0x604000074580>{number = 3, name = (null)} 2017-12-21 09:43:04.904603+0800 DeadThread[10647:3468266] 異步 + 串行 index 12 ::: <NSThread: 0x604000074580>{number = 3, name = (null)}?總結結果:::
1.異步: 開啟新的線程,不影響當前線程;
2.串行: 添加 任務 順序排列,順序執行
?
3.2.2? 異步 + 并行
- (void)asyncConcurrentQueue{dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_CONCURRENT);NSLog(@"異步 + 并行 start:::%@ ",[NSThread currentThread]);dispatch_async(queue, ^{for (int i = 0; i < 3; i++) {NSLog(@"異步 + 并行 index %d ::: %@",i,[NSThread currentThread]);}});dispatch_async(queue, ^{for (int i = 10; i < 13; i++) {NSLog(@"異步 + 并行 index %d ::: %@",i,[NSThread currentThread]);}});NSLog(@"同步 + 并行 end :::%@",[NSThread currentThread]); }?執行結果:::
2017-12-21 09:50:40.207852+0800 DeadThread[10727:3517966] 異步 + 并行 start:::<NSThread: 0x60c00006d880>{number = 1, name = main} 2017-12-21 09:50:40.208038+0800 DeadThread[10727:3517966] 同步 + 并行 end :::<NSThread: 0x60c00006d880>{number = 1, name = main} 2017-12-21 09:50:40.208045+0800 DeadThread[10727:3518055] 異步 + 并行 index 10 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)} 2017-12-21 09:50:40.208066+0800 DeadThread[10727:3518052] 異步 + 并行 index 0 ::: <NSThread: 0x600000075700>{number = 4, name = (null)} 2017-12-21 09:50:40.208139+0800 DeadThread[10727:3518055] 異步 + 并行 index 11 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)} 2017-12-21 09:50:40.208197+0800 DeadThread[10727:3518052] 異步 + 并行 index 1 ::: <NSThread: 0x600000075700>{number = 4, name = (null)} 2017-12-21 09:50:40.208327+0800 DeadThread[10727:3518055] 異步 + 并行 index 12 ::: <NSThread: 0x60400006f3c0>{number = 3, name = (null)} 2017-12-21 09:50:40.208361+0800 DeadThread[10727:3518052] 異步 + 并行 index 2 ::: <NSThread: 0x600000075700>{number = 4, name = (null)}?總結結果::::
1.異步:開啟線程能力;
2.并行:任務隊列不順序排列,同時執行;
3.開啟線程的數量:取決于添加任務的數量
圖示:::
轉載于:https://www.cnblogs.com/Bob-blogs/p/8078711.html
總結
以上是生活随笔為你收集整理的iOS 多线程的简单理解(3)执行方式 + 执行对列 的组合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java并发编程(十三)同步容器类
- 下一篇: 排序_简单排序_选择排序