iOS gcd 串行,并行,同步,异步代码研究
2017-02-10 18:14
621 查看
参考文章:
p.p1{margin:0.0px0.0px0.0px0.0px;font:13.0pxMenlo;color:#527eff}
span.s1{}
http://www.cnblogs.com/mddblog/p/4767559.html
重要结论:
p.p1{margin:0.0px0.0px0.0px0.0px;font:13.0px"PingFangSC";color:#4bd157}
p.p2{margin:0.0px0.0px0.0px0.0px;font:13.0pxMenlo;color:#4bd157;min-height:15.0px}
span.s1{font:13.0pxMenlo}
span.s2{}
1)串行队列,同步执行-----串行队列意味着顺序执行,同步执行意味着不开启线程(在当前线程执行)
2)串行队列,异步执行-----串行队列意味着任务顺序执行,异步执行说明要开线程,(如果开多个线程的话,不能保证串行队列顺序执行,所以只开一个线程)
3)并行队列,异步执行-----并行队列意味着执行顺序不确定,异步执行意味着会开启线程,而并行队列又允许不按顺序执行,所以系统为了提高性能会开启多个线程,来队列取任务(队列中任务取出仍然是顺序取出的,只是线程执行无序)。
4)并行队列,同步执行-----同步执行意味着不开线程,则肯定是顺序执行
分别进行验证
串行同步
结论:
串行同步,不创建任何新的线程,因为是在主线程中执行这个方法的,所以,同步方法都在主线程。
并行同步
结论:跟串行同步是一样的。
串行异步1
串行异步2
结论:
串行异步只创建一个新的线程
并行异步1
并行异步2
并行异步3
从并行异步的三个例子来看。
创建的新的线程的数目是不确定的,是跟队列中异步方法个数和异步方法执行的时间有关。
p.p1{margin:0.0px0.0px0.0px0.0px;font:13.0pxMenlo;color:#527eff}
span.s1{}
重要结论:
p.p1{margin:0.0px0.0px0.0px0.0px;font:13.0px"PingFangSC";color:#4bd157}
p.p2{margin:0.0px0.0px0.0px0.0px;font:13.0pxMenlo;color:#4bd157;min-height:15.0px}
span.s1{font:13.0pxMenlo}
span.s2{}
1)串行队列,同步执行-----串行队列意味着顺序执行,同步执行意味着不开启线程(在当前线程执行)
2)串行队列,异步执行-----串行队列意味着任务顺序执行,异步执行说明要开线程,(如果开多个线程的话,不能保证串行队列顺序执行,所以只开一个线程)
3)并行队列,异步执行-----并行队列意味着执行顺序不确定,异步执行意味着会开启线程,而并行队列又允许不按顺序执行,所以系统为了提高性能会开启多个线程,来队列取任务(队列中任务取出仍然是顺序取出的,只是线程执行无序)。
4)并行队列,同步执行-----同步执行意味着不开线程,则肯定是顺序执行
分别进行验证
串行同步
//串行同步 -(void)test_queue_serial_sync { NSLog(@"串行同步"); dispatch_queue_tqueue=dispatch_queue_create("com.demo.001",DISPATCH_QUEUE_SERIAL); NSLog(@"mainthread:%p",[NSThreadmainThread]); dispatch_sync(queue,^{ NSLog(@"1thread:%p",[NSThreadcurrentThread]); }); dispatch_sync(queue,^{ NSLog(@"2thread:%p",[NSThreadcurrentThread]); }); dispatch_sync(queue,^{ NSLog(@"3thread:%p",[NSThreadcurrentThread]); }); dispatch_sync(queue,^{ NSLog(@"4thread:%p",[NSThreadcurrentThread]); }); } //结果
2017-02-1018:00:21.991383RXVerifyExample[4927:2465789]串行同步 2017-02-1018:00:21.991636RXVerifyExample[4927:2465789]mainthread:0x17406cb00 2017-02-1018:00:21.992020RXVerifyExample[4927:2465789]1thread:0x17406cb00 2017-02-1018:00:21.992097RXVerifyExample[4927:2465789]2thread:0x17406cb00 2017-02-1018:00:21.992165RXVerifyExample[4927:2465789]3thread:0x17406cb00 2017-02-1018:00:21.992230RXVerifyExample[4927:2465789]4thread:0x17406cb00
结论:
串行同步,不创建任何新的线程,因为是在主线程中执行这个方法的,所以,同步方法都在主线程。
并行同步
//并行同步
-(void)test_queue_concurrent_sync
{
NSLog(@"并行同步");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.002",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_sync(queue,^{
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_sync(queue,^{
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果:
2017-02-1018:04:33.107907RXVerifyExample[4930:2467403]并行同步
2017-02-1018:04:33.108053RXVerifyExample[4930:2467403]mainthread:0x174066600
2017-02-1018:04:33.108121RXVerifyExample[4930:2467403]1thread:0x174066600
2017-02-1018:04:33.108177RXVerifyExample[4930:2467403]2thread:0x174066600
2017-02-1018:04:33.108219RXVerifyExample[4930:2467403]3thread:0x174066600
2017-02-1018:04:33.108259RXVerifyExample[4930:2467403]4thread:0x174066600
结论:跟串行同步是一样的。
串行异步1
//串行异步1
-(void)test_queue_serial_async1
{
NSLog(@"串行异步方法1");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.0031",DISPATCH_QUEUE_SERIAL);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_async(queue,^{
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果:
2017-02-1018:06:15.124048RXVerifyExample[4932:2468052]串行异步方法1
2017-02-1018:06:15.124244RXVerifyExample[4932:2468052]mainthread:0x174076a40
2017-02-1018:06:15.125490RXVerifyExample[4932:2468084]1thread:0x174271800
2017-02-1018:06:15.125574RXVerifyExample[4932:2468084]2thread:0x174271800
2017-02-1018:06:15.125643RXVerifyExample[4932:2468084]3thread:0x174271800
2017-02-1018:06:15.125710RXVerifyExample[4932:2468084]4thread:0x174271800
串行异步2
//串行异步2
-(void)test_queue_serial_async2
{
NSLog(@"串行异步方法2");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.0032",DISPATCH_QUEUE_SERIAL);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_async(queue,^{
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:4.1];
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:3.1];
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果
2017-02-1018:08:16.478508RXVerifyExample[4935:2468827]串行异步方法2
2017-02-1018:08:16.478611RXVerifyExample[4935:2468827]mainthread:0x170072380
2017-02-1018:08:16.479034RXVerifyExample[4935:2468869]1thread:0x174264240
2017-02-1018:08:16.479069RXVerifyExample[4935:2468869]2thread:0x174264240
2017-02-1018:08:20.584321RXVerifyExample[4935:2468869]3thread:0x174264240
2017-02-1018:08:23.689611RXVerifyExample[4935:2468869]4thread:0x174264240
结论:
串行异步只创建一个新的线程
并行异步1
//并行异步1
-(void)test_queue_concurrent_async1
{
NSLog(@"并行异步方法1");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.0041",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_async(queue,^{
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果
2017-02-1018:09:25.692681RXVerifyExample[4937:2469287]并行异步方法1
2017-02-1018:09:25.692880RXVerifyExample[4937:2469287]mainthread:0x174073000
2017-02-1018:09:25.693828RXVerifyExample[4937:2469323]1thread:0x174267d00
2017-02-1018:09:25.693916RXVerifyExample[4937:2469323]2thread:0x174267d00
2017-02-1018:09:25.693988RXVerifyExample[4937:2469323]3thread:0x174267d00
2017-02-1018:09:25.694056RXVerifyExample[4937:2469323]4thread:0x174267d00
并行异步2
//并行异步2
-(void)test_queue_concurrent_async2
{
NSLog(@"并行异步方法2");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.0042",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_async(queue,^{
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:4.1];
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:3.1];
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果
2017-02-1018:10:27.262009RXVerifyExample[4940:2469782]并行异步方法2
2017-02-1018:10:27.262213RXVerifyExample[4940:2469782]mainthread:0x17406e080
2017-02-1018:10:27.263710RXVerifyExample[4940:2469821]1thread:0x174262e80
2017-02-1018:10:27.263811RXVerifyExample[4940:2469821]2thread:0x174262e80
2017-02-1018:10:30.369209RXVerifyExample[4940:2469813]4thread:0x1742648c0
2017-02-1018:10:31.369205RXVerifyExample[4940:2469821]3thread:0x174262e80
并行异步3
//并行异步3
-(void)test_queue_concurrent_async3
{
NSLog(@"并行异步方法3");
dispatch_queue_tqueue=dispatch_queue_create("com.demo.0043",DISPATCH_QUEUE_CONCURRENT);
NSLog(@"mainthread:%p",[NSThreadmainThread]);
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:2.1];
NSLog(@"1thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:1.1];
NSLog(@"2thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:4.1];
NSLog(@"3thread:%p",[NSThreadcurrentThread]);
});
dispatch_async(queue,^{
[NSThreadsleepForTimeInterval:3.1];
NSLog(@"4thread:%p",[NSThreadcurrentThread]);
});
}
//结果
2017-02-1018:10:59.926988RXVerifyExample[4942:2470140]并行异步方法3
2017-02-1018:10:59.927184RXVerifyExample[4942:2470140]mainthread:0x17007f8c0
2017-02-1018:11:01.033209RXVerifyExample[4942:2470183]2thread:0x170462ac0
2017-02-1018:11:02.033478RXVerifyExample[4942:2470185]1thread:0x170462f40
2017-02-1018:11:03.037799RXVerifyExample[4942:2470176]4thread:0x170462a40
2017-02-1018:11:04.033691RXVerifyExample[4942:2470177]3thread:0x170462480
从并行异步的三个例子来看。
创建的新的线程的数目是不确定的,是跟队列中异步方法个数和异步方法执行的时间有关。
相关文章推荐
- iOS学习笔记74-IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别附代码
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别(附代码)
- iOS GCD中串行、并行、同步、异步执行顺序的研究
- iOS GCD/主队列/并行队列/全局队列/串行队列/同步任务/异步任务区别 含代码
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等(有示例代码)
- 【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
- iOS GCD, 同步,异步,串行队列,并行队列,dispatch_group
- ios使用GCD来执行同步或异步串行、并行操作。
- iOS-线程之GCD方式---之同步异步和串行队列并行队列之间的关系
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等
- iOS:对GCD中 同步、异步、并行、串行的见解
- 【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例
- iOS中多线程知识总结:进程、线程、GCD、串行队列、并行队列、全局队列、主线程队列、同步任务、异步任务等
- 【iOS开发-91】GCD的同步异步串行并行、NSOperation和NSOperationQueue一级用dispatch_once实现单例(转载)
- IOS多线程知识总结/队列概念/GCD/主队列/并行队列/全局队列/主队列/串行队列/同步任务/异步任务区别
- 理解 iOS 开发中 GCD 相关的同步(synchronization)\ 异步(asynchronization),串行(serial)\ 并行(concurrency)概念
- ios-day19-02(GCD介绍。串行队列、并行队列、全局队列、主队列、同步任务、异步任务)
- GCD的定义及使用详解(同步异步、并发串行、线程间通信、延时执行、只执行一次代码)
- iOS-GCD学习之同步异步线程去执行串行操作