iOS GCD
来源:互联网 发布:winxp升级win7软件 编辑:程序博客网 时间:2024/06/07 01:12
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
//1、GCD是最简单的一种多线程实现方式、同时也是执行效率最高的一种方式,(全部是用 c语言代码编写的 API),也是苹果公司最推崇的一种多线程实现方式。
//2、 GCD也是通过 queue 来实现多线程
//3、GCD里面有俩种 queue,一种是串行队列:serial queue ;还有一种是并行队列:
//-------------------------------------------------------------//
//serial queue(串行队列)特点:第一个任务执行完毕,第二个任务才可以开始,依次类推一个任务要开始,必须要等到上一个任务结束才可以开始。
//serial queue 有俩种获取方式
//第一种获取方式:里面的任务是在主线程依次去执行:dispatch_queue_t queue = dispatch_get_main_queue();
//第二种方式:自己创建的队列,第一个参数是队列的名字(苹果推荐使用反向域名去命名),第二个参数队列的类型(串行队列、并行队列),这种方式创建的队列,它会自己去开辟一个子线程去完成队列里面的任务 dispatch_queue_t queue = dispatch_queue_create("com.lanou3g.mySerialQueue", DISPATCH_QUEUE_SERIAL);
//-------------------------------------------------------------//
//并行队列--concurrent queue,特点:第一个任务开始之后,第二个任务不等第一个任务执行完毕就开始执行,依次类推,后边任务的执行跟前面没有关系。(先执行的任务不一定先执行完,最后执行的任务也不一定最后执行完),并行队列会根据队列里面的任务数量、cpu使用情况、内存消耗情况,开辟最合适的线程数量去完成队列里面的任务
// 并行队列也有两种创建方式
// 第一种方式 dispatch_queue_t oneQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// global queue 是苹果里面的全局队列,有4个优先级DISPATCH_QUEUE_PRIORITY_DEFAUL, DISPATCH_QUEUE_PRIORITY_HIGH, DISPATCH_QUEUE_PRIORITY_LOW, DISPATCH_QUEUE_PRIORITY_BACKGROUND
// 第一个参数就是队列的优先级,第二个参数是苹果预留的参数为了以后去使用,目前没有用到,填写0
// 第二种创建并发队列的方式: dispatch_queue_t twoQueue = dispatch_queue_create("com.lanou3g.myQueue", DISPATCH_QUEUE_CONCURRENT);
//-------------------------------------------------------------//
self.view.backgroundColor = [UIColor whiteColor];
UIButton *serialButton = [UIButton buttonWithType:UIButtonTypeSystem];
serialButton.frame = CGRectMake(100,100, 100,100);
serialButton.backgroundColor = [UIColor blueColor];
[serialButton setTitle:@"串行" forState:UIControlStateNormal];
[serialButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[self.view addSubview:serialButton];
[serialButton addTarget:self action:@selector(clickSerial) forControlEvents:UIControlEventTouchUpInside];
UIButton *concurrentButton = [UIButton buttonWithType:UIButtonTypeSystem];
concurrentButton.frame = CGRectMake(100,220, 100,100);
concurrentButton.backgroundColor = [UIColor blueColor];
[concurrentButton setTitle:@"并行" forState:UIControlStateNormal];
[concurrentButton setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
[self.view addSubview:concurrentButton];
[concurrentButton addTarget:self action:@selector(clickConcurrent) forControlEvents:UIControlEventTouchUpInside];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeSystem];
backButton.frame = CGRectMake(50,50, 50,50);
[backButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
[backButton setTitle:@"Back" forState:UIControlStateNormal];
[self.view addSubview:backButton];
[backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
}
- (void)back {
[self dismissViewControllerAnimated:YES completion:nil];
}
//串行
- (void)clickSerial {
/*
//第一种方式
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_async(queue, ^{
NSLog(@"小明出生了");
});
dispatch_async(queue, ^{
NSLog(@"小明长大了");
});
dispatch_async(queue, ^{
NSLog(@"小明结婚了");
});
dispatch_async(queue, ^{
NSLog(@"小明有娃了");
});
dispatch_async(queue, ^{
NSLog(@"小明老了");
});
dispatch_async(queue, ^{
NSLog(@"小明挂墙上了");
});
*/
//第二种方式
dispatch_queue_t queue = dispatch_queue_create("小明的一生", DISPATCH_QUEUE_SERIAL);
dispatch_async(queue, ^{
NSLog(@"小明出生了");
NSLog(@"。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明长大了");
NSLog(@"。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明结婚了");
NSLog(@"。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明有娃了");
NSLog(@"。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明老了");
NSLog(@"。。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明挂墙上了");
NSLog(@"。。。。。。%@", [NSThread currentThread]);
});
}
//并行
- (void)clickConcurrent {
/*
//第一种方式
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSLog(@"小明出生了");
NSLog(@"。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明长大了");
NSLog(@"。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明结婚了");
NSLog(@"。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明有娃了");
NSLog(@"。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明老了");
NSLog(@"。。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明挂墙上了");
NSLog(@"。。。。。。%@", [NSThread currentThread]);
});
*/
/*
//第二种方式
dispatch_queue_t queue = dispatch_queue_create("小明的一生", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
NSLog(@"小明出生了");
NSLog(@"。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明长大了");
NSLog(@"。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明结婚了");
NSLog(@"。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明有娃了");
NSLog(@"。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明有孙子");
NSLog(@"。。。。。%@", [NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"小明老了");
NSLog(@"。。。。。。%@", [NSThread currentThread]);
});
*/
//GCD中的group
dispatch_queue_t queue = dispatch_queue_create("12345", DISPATCH_QUEUE_CONCURRENT);
dispatch_group_t group = dispatch_group_create();
dispatch_group_async(group, queue, ^{
NSLog(@"1");
});
dispatch_group_async(group, queue, ^{
NSLog(@"2");
});
dispatch_group_async(group, queue, ^{
NSLog(@"3");
});
dispatch_group_async(group, queue, ^{
NSLog(@"4");
});
dispatch_group_async(group, queue, ^{
NSLog(@"5");
});
dispatch_group_notify(group, queue, ^{
NSLog(@"我总是最后一个");
});
//延迟执行
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"唔哈唔哈");
});
}
@end
- iOS gcd
- iOS gcd
- iOS gcd
- ios gcd
- IOS---GCD
- iOS GCD
- iOS GCD
- ios gcd
- ios GCD
- iOS -GCD
- ios gcd
- ios-GCD
- GCD IOS
- iOS-----GCD
- ios gcd
- iOS GCD
- ios GCD
- ios gcd
- [从头读历史] 第268节 诗经 鄘风
- centos下网络配置方法(网关、dns、ip地址配置)
- JS各种循环语句/条件语句的性能
- matlab-高数 极坐标系:阿基米德线,心形线的绘制
- C++ Primer Plus 第六版_编程练习(4)(Chapter_five 1-5)
- iOS GCD
- eclipse启动Tomcat时警告:WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property
- matlab-高数 无穷处极限
- Swift基础(八):析构过程、ARC、强弱引用、可空链式调用等
- truncate、delete与drop的区别
- iOS NSOperation
- java小知识点3
- 2-sum问题
- Python Demo