#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *action;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
//模拟从服务器上获取数据(返回字符串)
-(NSString *)loadingSomeThingFromServer
{
[NSThread sleepForTimeInterval:2];
return @"Hello wangdelong";
}
// 将传入参数全部转换成大写字母
-(NSString *)changname:(NSString *)result
{
[NSThread setThreadPriority:2];
return [result uppercaseString];
}
//模拟从服务器上第一次处理数据(将从服务器上返回的字符串长度转换成字符串:(Number of chars: )
-(NSString *)dealTheResult:(NSString *)result
{
[NSThread sleepForTimeInterval:3];
return [NSString stringWithFormat:@"long of zhe thinge is :%d",result.length];
}
//模拟第二次处理服务器上的数据,将返回的字符串里的小的w转换成大写的W
-(NSString *)dealTheNewThings:(NSString *)result
{
[NSThread sleepForTimeInterval:3];
return [result stringByReplacingOccurrencesOfString:@"W" withString:@"w"];
}
- (IBAction)doSomething:(UIButton *)sender {
// 声明一个表示时间的对象,[NSDate date]获取的是当前时间
NSDate *startTime = [NSDate date];
[self.action startAnimating];
// 创建一个队列,使其实现并发-----原以为能实现风火轮的准时停止,但是
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 异步发布
dispatch_async(queue, ^{
// //从服务器上取数据
__block NSString *result = nil;
dispatch_async(queue, ^{
// 处理事件
result = [self loadingSomeThingFromServer];
NSLog(@"********输入的是:%@",result);
});
result = [self changname:result];
// 第一次处理数据
NSString *second = [self dealTheResult:result];
// 第二次处理数据
NSString *third = [self dealTheNewThings:result];
NSLog(@"********大写的是:%@",result);
NSLog(@"********:%@",second);
NSLog(@"********修改后的是:%@",third);
NSDate *endTime = [NSDate date];
NSLog(@"共经过 %f m时间",[endTime timeIntervalSinceDate:startTime ]);
dispatch_sync(dispatch_get_main_queue(), ^{
[self.action stopAnimating];
});
});
}
//新添加的方法,显示最后的结果
-(void)showTheLastResult:(NSString *)first Second:(NSString *)second
{
NSLog(@"[First:]%@,[Second:]%@",first,second);
}
- (IBAction)doNewThing:(UIButton *)sender {
// 声明一个表示时间的对象,[NSDate date]获取的是当前时间
NSDate *startTime = [NSDate date];
[self.action startAnimating];
dispatch_queue_t queue =dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
// 异步分发队列
dispatch_async(queue, ^{
NSString *result =[self loadingSomeThingFromServer];
NSString *changName = [self changname:result];
NSLog(@"********输入的是:%@",result);
NSLog(@"********大写的是:%@",changName);
// GCD提供了dispatch group,可以将一组任务集合在一起,等待这组任务完成后再继续,
dispatch_group_t group =dispatch_group_create();
// __block这里是block的用法,为了能让block能传参数
__block NSString * first ;
__block NSString * second;
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
first = [self dealTheResult:changName];
NSLog(@"********:%@",first);
});
dispatch_group_async(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
second = [self dealTheNewThings:changName];
NSLog(@"********修改后的是:%@",second);
});
dispatch_group_notify(group, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self showTheLastResult:first Second:second];
dispatch_sync(dispatch_get_main_queue(), ^{
[self.action stopAnimating];
});
NSLog(@"共经过 %f m时间",[[NSDate date] timeIntervalSinceDate:startTime]);
});
});
}
@end