目录
在GCD中执行某个任务方法 必须要有一个任务和一个队列
1.消息循环
2.GCD基本演示
3.异步下载图片GCD改写
4.串行队列
5.并行队列
6.主队列
7.同步执行
8.全局并发执行
9.GCD综合练习
10.延时执行
11.队列组
12.一次性执行
13.单利模式
=======================================================
--------------------01GCD消息循环------------------------
========================================================
#import "ViewController.h"
#pragma --mark---消息循环
@interface ViewController ()
{
BOOL shouldKeepRunning ;//全局变量
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
shouldKeepRunning = NO;
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo2];
}
//主线程消息循环
-(void)demo{
//创建timer
//参数1:间隔时间
//参数2:对象
//参数3:方法
//参数4:自定义
//参数5:是否重复执行
NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:YES];
//把定时源加入到当前线程下消息循环中
//参数1:定时源
//参数2:模式
/**
现象:
NSDefaultRunLoopMode 拖动界面,定时源不运行
NSRunLoopCommonModes 拖动界面不受影响 一组模式的集合 一下都是其一种形式
没有拖动界面:kCFRunLoopDefaultMode
拖动界面:UITrackingRunLoopMode
*/
//拿到当前消息循环对象 [NSRunLoop currentRunLoop]
[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
-(void)task{
//输出当前循环的模式
NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
NSLog(@"task is running");
}
//子线程的消息循环
-(void)demo2{
//创建子线程
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(task2) object:nil];
[thread start];
//往指定线程的消息循环中加入源
//参数1:方法
//参数2:指定线程
//参数3:对象
//参数4:等待方法执行完成 子线程消息循环默认关闭
[self performSelector:@selector(addtask) onThread:thread withObject:nil waitUntilDone:NO];
}
//线程指定运行的方法
-(void)task2{
// NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
//输出当前循环的模式
NSLog(@"task2 is running %@",[NSThread currentThread]);
// 子线程消息循环默认关闭,开启消息循环 使用run方法后无法停止消息循环
// 面向对象编程, 首先要拿到消息循环对象
// [[NSRunLoop currentRunLoop] run];
//方法2:指定循环运行时间 同一份代码需要跑在不同的设备上 在不同设备有性能差别 定义一个固定时间不保险
// [[NSRunLoop currentRunLoop] runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
//方法3:apple推荐的方式(了解)
// BOOL shouldKeepRunning = YES; // global(全局)
NSRunLoop *theRL = [NSRunLoop currentRunLoop];
while (shouldKeepRunning && [theRL runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]);
NSLog(@"over");
}
//消息循环中源的方法
-(void)addtask{
NSLog(@"addtask is running");
}
@end
========================================================
--------------------01GCD基本练习------------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo];
}
//GCD基本演示
-(void)demo{
// void (^mytask)(void) = ^{};
//创建任务
// dispatch_block_t task = ^{
// NSLog(@"task %@",[NSThread currentThread]);
// };
//
// //获取队列
// dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
//
// //把任务放到队列里面
// //参数1:队列
// //参数2:任务
// dispatch_async(queue, task);
//
//
// //简单写法 工作中常用代码
// dispatch_async(dispatch_get_global_queue(0, 0), ^{
//
// });
//多个任务
for (int i = 0; i < 20; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSLog(@"task %@ %d",[NSThread currentThread],i);
});
}
}
@end
========================================================
--------------------01GCD异步下载图片----------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) UIImageView *testImageView;
@property (weak, nonatomic) UIScrollView *scrollView;
@end
@implementation ViewController
//重写此方法,默认不会加载xib&sb
-(void)loadView{
UIScrollView *sc = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.scrollView = sc;
self.view = sc;
UIImageView *imageView = [[UIImageView alloc] init];
self.testImageView = imageView;
[self.view addSubview: self.testImageView];
}
- (void)viewDidLoad {
[super viewDidLoad];
dispatch_async(dispatch_get_global_queue(0, 0), ^{
NSString *imgAddr = @"http://img.bimg.126.net/photo/rl0IM2SIJK8jWXgIgxhJsw==/2871889187379358521.jpg";
NSURL *url = [NSURL URLWithString:imgAddr];
NSData *data = [NSData dataWithContentsOfURL:url];
//闭包
UIImage *image = [UIImage imageWithData:data];
//线程通信
dispatch_async(dispatch_get_main_queue(), ^{
self.testImageView.image = image;
[self.testImageView sizeToFit];
[self.scrollView setContentSize:image.size];
});
});
}
//
下载图片
//-(void)downloadIMG{
// NSLog(@"downloadimg %@",[NSThread currentThread]);
//
// //网络卡
[NSThread sleepForTimeInterval:3];
//
// NSString *imgAddr = @"http://img.bimg.126.net/photo/rl0IM2SIJK8jWXgIgxhJsw==/2871889187379358521.jpg";
// NSURL *url = [NSURL URLWithString:imgAddr];
// //获取到图片的原始数据
// NSData *data = [NSData dataWithContentsOfURL:url];
// UIImage *image = [UIImage imageWithData:data];
//
// //线程间通信 子->主
// //参数1:方法
// //参数2:方法的参数
// //参数3:等待执行完成
// [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];
//
// NSLog(@"over");
//}
//
更新UI
//-(void)updateUI:(UIImage *)image{
// [NSThread sleepForTimeInterval:1];
// NSLog(@"updateUI %@",[NSThread currentThread]);
// self.testImageView.image = image;
// //根据图片的大小来调整位置
// [self.testImageView sizeToFit];
// [self.scrollView setContentSize:image.size];
//}
@end
========================================================
--------------------01GCD串行队列------------------------
========================================================
#pragma---mark---串行队列
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo2];
}
* @discussion
* Dispatch queues created with the DISPATCH_QUEUE_SERIAL or a NULL attribute
* invoke blocks serially in FIFO order.
*
* Dispatch queues created with the DISPATCH_QUEUE_CONCURRENT attribute may
//串行队列,同步执行
//不开线程,在当前线程下执行(主线程)
//任务是有序执行的
-(void)demo1{
//创建串行队列
//参数1:队列的名字
//参数2:队列的属性
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
for (int i =0; i<20; i++) {
dispatch_sync(serialQueue, ^{
NSLog(@"#######serialQueue %@ %d",[NSThread currentThread],i);
});
}
}
//串行队列,异步执行
//开一个线程(开启一个子线程)
//任务是有序执行的
-(void)demo2{
//创建串行队列
//参数1:队列的名字
//参数2:队列的属性
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
for (int i =0; i<20; i++) {
dispatch_async(serialQueue, ^{
NSLog(@"serialQueue %@ %d",[NSThread currentThread],i);
});
}
}
@end
========================================================
--------------------01GCD并行队列------------------------
========================================================
//
// ViewController.m
// 08并行队列
//
// Created by apple on 15/10/9.
// Copyright © 2015年 apple. All rights reserved.
//
#pragma---mark---并行队列
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo2];
}
//并行队列,异步执行
//开多个线程
//任务无序执行
//效率最大
-(void)demo1{
//生成并行队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 20; i++) {
dispatch_async(concurrentQueue, ^{
NSLog(@"con %@ %d",[NSThread currentThread],i);
});
}
}
//并行队列,同步执行
//不开线程,在当前线程下执行
//任务顺序执行
//等于串行队列,同步执行
-(void)demo2{
//生成并行队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
for (int i = 0; i < 20; i++) {
dispatch_sync(concurrentQueue, ^{
NSLog(@"con %@ %d",[NSThread currentThread],i);
});
}
}
@end
========================================================
--------------------01GCD主队列---------------------------
========================================================
//
// ViewController.m
// 09主队列
//
// Created by apple on 15/10/9.
// Copyright © 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo2];
}
//主队列,异步执行
//在主线线程执行
//任务顺序执行
-(void)demo1{
//得到主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
for (int i = 0; i < 20; i++) {
dispatch_async(mainQueue, ^{
NSLog(@"main queue %@ %d",[NSThread currentThread],i);
});
}
}
//主队列,同步执行
//死锁:主线程下,往主队列添加任务并同步执行
-(void)demo2{
NSLog(@"begin");
//得到主队列
dispatch_queue_t mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
NSLog(@"main queue %@",[NSThread currentThread]);
});
NSLog(@"end");
}
@end
========================================================
--------------------01GCD同步执行------------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//1.下载LOL1.zip
//2.下载LOL2.zip
//3.通知UI
/**
分析:1,2子线程运行
3.主线程运行
1,2一定在3之前
*/
[self demo2];
}
//串行队列,异步执行-开一个线程,任务顺序执行
-(void)demo1{
//创建串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
//模拟网络延时
[NSThread sleepForTimeInterval:arc4random_uniform(3)];
NSLog(@"下载 LOL1.zip %@",[NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
[NSThread sleepForTimeInterval:arc4random_uniform(3)];
NSLog(@"下载 LOL2.zip %@",[NSThread currentThread]);
});
dispatch_async(serialQueue, ^{
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"下载完成%@",[NSThread currentThread]);
});
});
}
//并发队列 异步开启一个线程, 在这个线程中执行同步任务 ,执行结束返回主线程刷新UI
-(void)demo2{
//创建并发队列
dispatch_queue_t concurrentQueue = dispatch_queue_create("concurrentQueue", DISPATCH_QUEUE_CONCURRENT);
//开启1个线程
dispatch_async(concurrentQueue, ^{
NSLog(@"concurrentQueue %@",[NSThread currentThread]);
dispatch_sync(concurrentQueue, ^{
NSLog(@"下载 LOL1.zip %@",[NSThread currentThread]);
});
dispatch_sync(concurrentQueue, ^{
NSLog(@"下载 LOL2.zip %@",[NSThread currentThread]);
});
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"下载完成%@",[NSThread currentThread]);
});
});
}
@end
========================================================
--------------------01GCD全局并发队列---------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo];
}
//全局并发队列
-(void)demo{
//参数1:优先级identifier qos_class_t(iOS8),
//iOS8 之前
//dispatch_queue_priority_t
//#define DISPATCH_QUEUE_PRIORITY_HIGH 2
//#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
//#define DISPATCH_QUEUE_PRIORITY_LOW (-2)
//#define DISPATCH_QUEUE_PRIORITY_BACKGROUND INT16_MIN
//qos_class_t(iOS8)
// QOS_CLASS_USER_INTERACTIVE, QOS_CLASS_USER_INITIATED, QOS_CLASS_UTILITY, or QOS_CLASS_BACKGROUND,QOS_CLASS_UNSPECIFIED(0).
//参数2:flags 没有用,给未来使用 填0即可
dispatch_queue_t queue = dispatch_get_global_queue(0, 0);
}
@end
========================================================
--------------------12GCD综合联系---------------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// 任务1,2在子线程顺序执行
// 完成之后通知主线程执行任务3,4并顺序执行
// 完成之后开启任务5,6,7并发执行
[self demo];
}
-(void)demo{
/**
任务1,2在子线程顺序执行
分析:任务1,2在子线程顺序执行->串行队列,异步执行
*/
//创建串行队列
dispatch_queue_t serialQueue = dispatch_queue_create("serialQueue", DISPATCH_QUEUE_SERIAL);
dispatch_async(serialQueue, ^{
[self task:1];
});
dispatch_async(serialQueue, ^{
[self task:2];
});
/*
完成之后通知主线程执行任务3,4并顺序执行
分析:先往主队列放两个任务 在考虑其他 3,4任务在2之后再做 把3,4看成一个任务放到一个队列里边就行了
*/
dispatch_sync(serialQueue, ^{//这个block的任务一定在2之后
/*
dispatch_sync(serialQueue 串行队列同步执行 仅仅是最外面的花括号在当前线程下执行
和在一个子线程中执行对我花括号里边的3,4在主线程中执行没有影响
task:8在主线程执行
dispatch_async(serialQueue 串行队列异步执行
task:8在子线程执行
async不会等block的任务执行完再执行后面的任务 下面的5.6.7可能同时执行
*/
// [self task:8];
dispatch_async(dispatch_get_main_queue(), ^{
[self task:3];
});
dispatch_async(dispatch_get_main_queue(), ^{
[self task:4];
});
});
/*
完成之后开启任务5,6,7并发执行
分析:全局并发队列,异步执行
主线程保证了任务顺序执行
**/
/**
完成之后开启任务5,6,7并发执行
并行队列 异步执行效率最高 既然系统有并发队列就用系统的 不同再自己创建了
5.6.7在4后面执行 4在主线程执行 主线程是一个特殊的串行队列 直接在主线程后面加一个任务就行了
分析:全局队列,异步执行
主线程保证了任务顺序执行
*/
dispatch_async(dispatch_get_main_queue(), ^{
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self task:5];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self task:6];
});
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self task:7];
});
});
}
-(void)task:(int)number{
NSLog(@"%@ task %d",[NSThread currentThread],number);
}
@end
========================================================
--------------------13GCD延时执行------------------------
========================================================
//
// ViewController.m
// 13延时执行
//
// Created by apple on 15/10/9.
// Copyright © 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo];
}
//延时执行 选取 dispatch_after GCD方法
-(void)demo{
//方式1:timer
// NSTimer *timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(task) userInfo:nil repeats:NO];
//
// //方式2
// [self performSelector:@selector(task) withObject:nil afterDelay:1];
//前两个方法以秒为计算单位不好把握
//方法3
//参数1:延时的时间 dispatch_time生成时间 纳秒为计时单位 精度高
//参数2:队列
//参数3:任务
//异步执行 PER每 SEC秒 NSEC纳秒
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"task");//异步执行 over 不等task执行完再执行
});
NSLog(@"over");
}
@end
========================================================
--------------------14GCD队列组---------------------------
========================================================
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[self demo2];
}
//队列组
-(void)demo{
NSLog(@"begin");
//创建组
dispatch_group_t group = dispatch_group_create();
//开启异步任务
//参数1:组
//参数2:队列
//参数3:任务
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//模拟网络卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)];
NSLog(@"LOL1.zip %@",[NSThread currentThread]);
});
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
//模拟网络卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)];
NSLog(@"LOL2.zip %@",[NSThread currentThread]);
});
//参数1:group
//参数2:队列
//参数3:任务
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"下载完成 %@",[NSThread currentThread]);
});
}
-(void)demo2{
//在终端输入man dispatch_group_async后会输出
// dispatch_group_async(dispatch_group_t group, dispatch_queue_t queue, dispatch_block_t block)
// {
// dispatch_retain(group);
// dispatch_group_enter(group);
// dispatch_async(queue, ^{
// block();
// dispatch_group_leave(group);
// dispatch_release(group);
// });
// }
dispatch_group_t group = dispatch_group_create();
// dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
// //模拟网络卡
// [NSThread sleepForTimeInterval:arc4random_uniform(5)];
// NSLog(@"LOL1.zip %@",[NSThread currentThread]);
// });
dispatch_group_enter(group);
dispatch_async( dispatch_get_global_queue(0, 0), ^{
//模拟网络卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)];
NSLog(@"LOL1.zip %@",[NSThread currentThread]);
dispatch_group_leave(group);
});
dispatch_group_enter(group);
dispatch_async( dispatch_get_global_queue(0, 0), ^{
//模拟网络卡
[NSThread sleepForTimeInterval:arc4random_uniform(5)];
NSLog(@"LOL2.zip %@",[NSThread currentThread]);
dispatch_group_leave(group);
});
//参数1:group
//参数2:队列
//参数3:任务
dispatch_group_notify(group, dispatch_get_main_queue(), ^{
NSLog(@"下载完成 %@",[NSThread currentThread]);
});
}
@end
========================================================
--------------------15GCD一次性执行-----------------------
========================================================
//
// ViewController.m
// 15一次性执行
//
// Created by apple on 15/10/9.
// Copyright © 2015年 apple. All rights reserved.
//
#import "ViewController.h"
@interface ViewController ()
{
int num;//多线程num不安全
}
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// for (int i = 0; i < 20; i++) {
// [self demo];
// }
//多线程测试
for (int i = 0; i < 20; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
[self demo];
});
}
}
//一次性执行
//线程安全的
-(void)demo{
NSLog(@"demo--->");
/*
dispatch_once_t long//静态区长整形数字 把数字地址传入方法
*/
/*
2016-06-04 12:59:07.552 GCD练习[11538:184718] #########demo
2016-06-04 12:59:07.555 GCD练习[11538:184718] ---->>>0
2016-06-04 12:59:07.556 GCD练习[11538:184718] demo
2016-06-04 12:59:07.556 GCD练习[11538:184718] #########demo
2016-06-04 12:59:07.557 GCD练习[11538:184718] ---->>>-1
2016-06-04 12:59:07.557 GCD练习[11538:184718] #########demo
2016-06-04 12:59:07.558 GCD练习[11538:184718] ---->>>-1
2016-06-04 12:59:07.558 GCD练习[11538:184718] #########demo
2016-06-04 12:59:07.560 GCD练习[11538:184718] ---->>>-1
2016-06-04 12:59:07.560 GCD练习[11538:184718] #########demo
*/
static dispatch_once_t onceToken;
NSLog(@"%ld",onceToken);
dispatch_once(&onceToken, ^{
NSLog(@"once");
});
}
@end
========================================================
--------------------01GCD单利模式---------------------------
========================================================
#import <Foundation/Foundation.h>
@interface CZConfig : NSObject
//返回唯一的一个对象--单例:整个系统中只有唯一的一个类的对象
+(instancetype)config;
//同步锁实现单例
+(instancetype)configSync;
@end
#import "CZConfig.h"
@implementation CZConfig
+(instancetype)config{
static CZConfig *instance;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
instance = [[CZConfig alloc] init];
});
return instance;
}
+(instancetype)configSync{
static CZConfig *instance;
@synchronized(self) {
if(instance == nil){
instance = [[CZConfig alloc] init];
}
}
return instance;
}
@end
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController
@end
#import "ViewController.h"
#import "CZConfig.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
// CZConfig *config1 = [[CZConfig alloc] init];
// NSLog(@"config1 %@",config1);
// CZConfig *config2 = [[CZConfig alloc] init];
// NSLog(@"config2 %@",config2);
// CZConfig *config3 = [CZConfig config];
// NSLog(@"config3 %@",config3);
// CZConfig *config4 = [CZConfig config];
// NSLog(@"config4 %@",config4);
//
// CZConfig *config5 = [CZConfig configSync];
// NSLog(@"config5 %@",config5);
// CZConfig *config6 = [CZConfig configSync];
// NSLog(@"config6 %@",config6);
[self demo];
}
//性能检测
-(void)demo{
//dispatch_once 性能高 没有等待性能消耗 能手写 dispatch_once是线程安全的
double time1 = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < 999*999; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
CZConfig *config3 = [CZConfig config];
});
}
double time2 = CFAbsoluteTimeGetCurrent();
NSLog(@"%f",time2-time1);
// 同步锁
double time3 = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < 999*999; i++) {
dispatch_async(dispatch_get_global_queue(0, 0), ^{
CZConfig *config3 = [CZConfig configSync];
});
}
double time4 = CFAbsoluteTimeGetCurrent();
NSLog(@"%f",time4-time3);
}
@end