通过对前面两偏线程理解的总结,自己对线程的理解也逐渐加深,梳理的清晰起来……
通常在使用线程 的时候,都是要用到 执行对列,执行方式,执行任务,
现在开始新一轮的深入
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]); }
执行结果:::
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.开启线程的数量:取决于添加任务的数量
图示:::
时间: 2024-11-09 16:46:39