多线程:同步的作用

来源:互联网 发布:java软件工程师就业 编辑:程序博客网 时间:2024/06/16 08:55
////  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
0 0
原创粉丝点击