經過對前面兩偏線程理解的總結,本身對線程的理解也逐漸加深,梳理的清晰起來……異步
一般在使用線程 的時候,都是要用到 執行對列,執行方式,執行任務,async
如今開始新一輪的深刻spa
3. 1. 1 同步 + 串行線程
- (void)syncSerialQueue{ dispatch_queue_t queue = dispatch_queue_create("test", DISPATCH_QUEUE_SERIAL); NSLog(@" 同步 + 串行 start:::%@ ",[NSThread currentThread]); dispatch_sync(queue, ^{ // 添加任務 1 for (int i = 0; i < 3; i++) { NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]); } }); dispatch_sync(queue, ^{ // 添加任務 2 for (int i = 10; i < 13; i++) { NSLog(@"同步 + 串行 index %d ::: %@",i,[NSThread currentThread]); } }); NSLog(@"同步 + 串行 end :::%@",[NSThread currentThread]); }
執行結果:::3d
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}
總結結果:::blog
1. 同步 : 在當前線程執行,不開啓新的線程,任務順序執行隊列
2. 串行 :添加的任務 順序排列,順序執行同步
圖示::::class
3.1.2 同步 + 並行test
- (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.開啓線程的數量:取決於添加任務的數量
圖示:::