226,GCD实例演示
来源:互联网 发布:大数据对日常生活影响 编辑:程序博客网 时间:2024/04/30 08:02
#import "ViewController.h"
@interface ViewController ()
@property (nonatomic,strong)dispatch_queue_t concurrentQueue;
@property (nonatomic,strong)dispatch_queue_t serialQueue;
@property (nonatomic,strong)dispatch_queue_t mainQueue;
@end
@implementation ViewController
- (dispatch_queue_t)concurrentQueue{
if (_concurrentQueue ==nil) {
_concurrentQueue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0);
}
return_concurrentQueue;
}
- (dispatch_queue_t)serialQueue{
if (_serialQueue ==nil) {
_serialQueue =dispatch_queue_create("cn.ljs",NULL);;
}
return_serialQueue;
}
- (dispatch_queue_t)mainQueue{
if (_mainQueue ==nil) {
_mainQueue =dispatch_get_main_queue();
}
return_mainQueue;
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//[self asynchronousConcurrent];
//[self asynchronouseSerial];
//[self asynchronouseMain];
//[self synchronousConcurrent];
[selfsynchronouseSerial];
}
//异步,并发队列:开了多条子线程,同时执行多个任务
- (void)asynchronousConcurrent{
dispatch_async(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);
});
dispatch_async(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);
});
dispatch_async(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);
});
dispatch_async(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);
});
dispatch_async(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);
});
}
//2016-01-21 13:03:57.206 5,GCD实例[1056:110598] -----<NSThread: 0x7fabd0e032a0>{number = 5, name = (null)}-------下载图片4--------
//2016-01-21 13:03:57.206 5,GCD实例[1056:110599] -----<NSThread: 0x7fabd0d1f290>{number = 6, name = (null)}-------下载图片5--------
//2016-01-21 13:03:57.206 5,GCD实例[1056:110385] -----<NSThread: 0x7fabd2904550>{number = 4, name = (null)}-------下载图片3--------
//2016-01-21 13:03:57.206 5,GCD实例[1056:110384] -----<NSThread: 0x7fabd2b0e2c0>{number = 3, name = (null)}-------下载图片2--------
//2016-01-21 13:03:57.206 5,GCD实例[1056:110383] -----<NSThread: 0x7fabd0e03890>{number = 2, name = (null)}-------下载图片1--------
//异步,串行队列:会创建一条子线程,按照队列顺序,任务一个接着一个地执行
- (void)asynchronouseSerial{
dispatch_async(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);
});
dispatch_async(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);
});
dispatch_async(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);
});
dispatch_async(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);
});
dispatch_async(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);
});
}
//2016-01-21 13:20:54.017 5,GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片1--------
//2016-01-21 13:20:54.018 5,GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片2--------
//2016-01-21 13:20:54.018 5,GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片3--------
//2016-01-21 13:20:54.018 5,GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片4--------
//2016-01-21 13:20:54.018 5,GCD实例[1295:122912] -----<NSThread: 0x7fdc9051ca20>{number = 2, name = (null)}-------下载图片5--------
//异步,主队列(主队列就是一个串行队列):不会创建子线程,只在主线程中按照串行队列顺序一个接着一个地执行
- (void)asynchronouseMain{
dispatch_async(self.mainQueue, ^{
NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);
});
dispatch_async(self.mainQueue, ^{
NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);
});
dispatch_async(self.mainQueue, ^{
NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);
});
dispatch_async(self.mainQueue, ^{
NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);
});
dispatch_async(self.mainQueue, ^{
NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);
});
}
//2016-01-21 13:29:49.524 5,GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片1--------
//2016-01-21 13:29:49.525 5,GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片2--------
//2016-01-21 13:29:49.525 5,GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片3--------
//2016-01-21 13:29:49.526 5,GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片4--------
//2016-01-21 13:29:49.526 5,GCD实例[1383:127064] -----<NSThread: 0x7fc2e0f034a0>{number = 1, name = main}-------下载图片5--------
//同步,并发队列:不会创建子线程,只在主线程中按照串行队列顺序一个接着一个地执行
- (void)synchronousConcurrent{
dispatch_sync(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.concurrentQueue, ^{
NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);
});
}
//2016-01-21 13:36:41.269 5,GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片1--------
//2016-01-21 13:36:41.270 5,GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片2--------
//2016-01-21 13:36:41.270 5,GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片3--------
//2016-01-21 13:36:41.270 5,GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片4--------
//2016-01-21 13:36:41.270 5,GCD实例[1439:130167] -----<NSThread: 0x7fe2d4104ce0>{number = 1, name = main}-------下载图片5--------
//同步,串行队列:会创建一条子线程,按照队列顺序,任务一个接着一个地执行
- (void)synchronouseSerial{
dispatch_sync(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片1--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片2--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片3--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片4--------",[NSThreadcurrentThread]);
});
dispatch_sync(self.serialQueue, ^{
NSLog(@"-----%@-------下载图片5--------",[NSThreadcurrentThread]);
});
}
//2016-01-21 13:39:16.764 5,GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片1--------
//2016-01-21 13:39:16.764 5,GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片2--------
//2016-01-21 13:39:16.765 5,GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片3--------
//2016-01-21 13:39:16.765 5,GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片4--------
//2016-01-21 13:39:16.765 5,GCD实例[1502:133241] -----<NSThread: 0x7fa2a42037f0>{number = 1, name = main}-------下载图片5--------
//同步,主队列:会死锁,不能用
//总结:前三种方法是常用,需要掌握
@end
- 226,GCD实例演示
- 多线程GCD演示
- GCD实例
- Xquery实例演示
- 8.2.2 实例演示
- jQuery Ajax 实例演示
- jQuery Ajax 实例演示
- jQuery Ajax 实例演示
- jQuery Ajax 实例演示
- 照片墙一多实例演示
- Hibernate 实例演示
- 自定义ContentProvider 实例演示
- jQuery Ajax 实例演示
- EHcache实例演示
- NAT配置实例演示
- Makefile编写------------实例演示
- jQuery Ajax 实例演示
- XPath实例演示
- Java 单例模式详解
- qt 上下页布局与代码规范
- 促销体系之单次购物券
- android---listView分割线的显示和隐藏
- Leetcode 301. Remove Invalid Parentheses
- 226,GCD实例演示
- 什么是尾递归(python引例)
- jQuery Ajax 实例 全解析
- RH413企业安全加固 第10章 第二节 管理 PAM
- 促销体系之商品预售
- 网络加密解密原理(二) RSA加密解密及数字签名Java实现
- 1104. Sum of Number Segments (20)
- 解决"应用程序无法启动,因为应用程序的并行配置不正确"问题
- 促销体系之积分赠送