您的位置:首页 > 其它

多线程:同步的作用

2016-03-25 23:27 225 查看
//
//  ViewController.m
//  10-同步的作用
//
//  Created by gzxzmac on 16/1/29.
//  Copyright © 2016年 gzxzmac. 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 gcdDemo];
}
/*
1. 同步的作用:先做某些事情,做完之后再去做其他的(比如先登录,再下载(扣费))
*/
// 先登录再下载
- (void)gcdDemo {
// 创建并发队列
dispatch_queue_t queue = dispatch_queue_create("itcast", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{
// 登录
dispatch_sync(queue, ^{// 如果网络慢,
NSLog(@"登录..%@", [NSThread currentThread]);
});

// 下载文件A,B
dispatch_async(queue, ^{
NSLog(@"下载文件A %@",[NSThread currentThread]);
});
dispatch_async(queue, ^{
NSLog(@"下载文件B %@",[NSThread currentThread]);
});

});

}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: