GCD
来源:互联网 发布:linux服务器编程 pdf 编辑:程序博客网 时间:2024/05/18 15:31
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning {
[superdidReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark --多线程之 NSThread
- (IBAction)NSThreadButton:(UIButton *)sender
{
// 自定义多线程
NSThread *thread = [[NSThreadalloc] initWithTarget:selfselector:@selector(action:)object:@"大水贝"];
// 开启多线程
[threadstart];
// 使用系统的
[NSThreaddetachNewThreadSelector:@selector(downloadImage)toTarget:selfwithObject:nil];
NSLog(@"当前线程为 %@是否是主线程 %d", [NSThreadcurrentThread], [NSThreadisMainThread]);
}
- (void)downloadImage
{
NSString *urlStr =@"http://amuse.nen.com.cn/imagelist/11/21/9as70n3ir61b.jpg";
NSURL *url = [NSURLURLWithString:urlStr];
NSData *data = [NSDatadataWithContentsOfURL:url];
self.NSThreadImageView.image = [UIImageimageWithData:data];
}
- (void)action:(NSString *)str
{
NSLog(@"当前线程为 %@是否是主线程 %d", [NSThreadcurrentThread], [NSThreadisMainThread]);
NSLog(@"str = %@", str);
}
#pragma mark --多线程之 NSOperation
- (IBAction)NSOperation:(UIButton *)sender
{
// NSOperation 是抽象类, 我们用它提供的两个子类
//NSInvocationOperation 创建的一个对象就是一个任务
NSInvocationOperation *operation = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(action:)object:@"大水表"];
NSInvocationOperation *operation1 = [[NSInvocationOperationalloc] initWithTarget:selfselector:@selector(action1)object:nil];
NSBlockOperation *blockOperation = [NSBlockOperationblockOperationWithBlock:^{
// for (int i = 0; i < 100; i++) {
// NSLog(@"i = %d", i);
// }
}];
//队列 用来管理队列里的一个或者多个任务
NSOperationQueue *queue = [[NSOperationQueuealloc] init];
//最大允许并发执行任务的个数
[queue setMaxConcurrentOperationCount:2];
//绑定一个任务, 就是前者必须等后者执行完才能执行
[operationaddDependency:blockOperation];
//将任务加入到队列中
[queueaddOperation:operation];
[queueaddOperation:operation1];
[queueaddOperation:blockOperation];
}
- (void)action1
{
NSLog(@"action1");
}
#pragma mark --GCD之串行 serial
- (IBAction)GCDButton:(UIButton *)sender
{
//串行特点 一个一个执行,一个执行完再执行下一个
// 1.系统自带串行队列
// 创建系统主队列
#warning 重点经常用到,作用从子线程上得到主线程
// dispatch_queue_t mainQueue = dispatch_get_main_queue();
//
// //1.2 执行主队列
// dispatch_async(mainQueue, ^{
//
// NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// NSLog(@"3 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
#warning 重要 非常常见的写法
// dispatch_async(dispatch_get_main_queue(), ^{
//
// NSLog(@"获得主线程的大众写法");
// });
//
// // 2.自定义串行队列
// // 2.1 创建自定义串行队列
// // 参数1:队列标示符 一般是逆向域名
// // 参数2: 队列类型
// dispatch_queue_t mySerialQueue = dispatch_queue_create("com.lanou3g.GCD.Serial", DISPATCH_QUEUE_SERIAL);
// // 2.2 执行队列
// dispatch_async(mySerialQueue, ^{
//
// NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// NSLog(@"3 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// NSLog(@"4 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
#pragma mark --GCD之 并行
#warning 重要 经常跟dispatch_get_main_queue 一起使用
// // 1.得到全局队列
// // 参数1.队列优先级
// // 参数2.苹果预留参数,默认给0
// dispatch_queue_t globalQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// // 2.执行队列
// dispatch_async(globalQueue, ^{
//
// NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
//
// });
// dispatch_async(globalQueue, ^{
//
// NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
//
// });
// 自定义并行队列
//创建自定义并行队列
// dispatch_queue_t myCyurrentQueue = dispatch_queue_create("com.lanou3g.GCD.ConcurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//
// // 2.执行并行队列
// dispatch_async(myCyurrentQueue, ^{
//
// NSLog(@"1 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
//
//
// dispatch_async(myCyurrentQueue, ^{
//
// NSLog(@"2 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
//
// dispatch_async(myCyurrentQueue, ^{
//
// NSLog(@"3 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
//
// dispatch_async(myCyurrentQueue, ^{
//
// NSLog(@"4 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
//
//
// dispatch_async(myCyurrentQueue, ^{
//
// NSLog(@"5 = %@ %d", [NSThread currentThread], [NSThread isMainThread]);
// });
//
#warning 重要
//练习 子线程请求图片主线程负责刷新显示图片
// 1.dispatch_get_global_queue 创建系统并行队列
// 2.dispatch_async 执行队列
// dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
// // 此块是子线程
// NSString *urlStr = @"http://amuse.nen.com.cn/imagelist/11/21/9as70n3ir61b.jpg";
// NSURL *url = [NSURL URLWithString:urlStr];
// NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *image = [UIImage imageWithData:data];
//
// __block ViewController *vc = self;
// // 在主线程显示图片
// dispatch_async(dispatch_get_main_queue(), ^{
//
// NSLog(@"%@ %d", [NSThread currentThread], [NSThread isMainThread]);
// vc.NSThreadImageView.image = image;
// });
//
// });
#warning 重要
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5.0 * NSEC_PER_SEC)),dispatch_get_main_queue(), ^{
NSLog(@"1 = %@ %d", [NSThreadcurrentThread], [NSThreadisMainThread]);
});
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
NSLog(@"2 = %@ %d", [NSThreadcurrentThread], [NSThreadisMainThread]);
});
}
@end
- gcd
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- GCD
- gcd
- GCD
- GCD
- GCD
- GCD
- GCD
- 嵌入式中的 *(volatile unsigned int *)0x500 解释
- 存储引擎
- openssl windows编译 32位&64位
- Ubuntu 14.04 下启动 VirtualBox里面的win7 出现vboxdrv模块加载失败的情况
- JBPM
- GCD
- linux权限--普通用户使用livecd模式
- 事务的编写
- windows下 pip安装
- js设置ie浏览器的文档模式为当前最高
- Association Class VS Full Class
- 如何统计每个小时用户在线人数?
- Enum的例子
- spring MVC的困惑--url-pattern的/和/*有区别