码迷,mamicode.com
首页 > 其他好文 > 详细

同步函数

时间:2015-09-02 08:15:22      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:

#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 *)touches withEvent:(UIEvent *)event

{

    NSLog(@"touchesBegan:%@",[NSThread currentThread]);

    

    NSLog(@"耗时操作1,%@",[NSThread currentThread]);

    

    NSLog(@"耗时操作2,%@",[NSThread currentThread]);

    

    NSLog(@"耗时操作3,%@",[NSThread currentThread]);

    

    NSLog(@"touchesEnd:%@",[NSThread currentThread]);

 

}

 

// 同步函数 + 主队列

// 结论:主线程中的任务和主队列中的任务相互等待.谁也无法执行.

- (void)test3

{

    // 同步函数 + 主队列

    // 结论:主线程中的任务和主队列中的任务相互等待.谁也无法执行.

    

    dispatch_sync(dispatch_get_main_queue(), ^{

        

        NSLog(@"耗时操作1,%@",[NSThread currentThread]);

    });

}

 

// 同步函数+并发队列

// 结论:1.主线程执行/没有开启新线程

//      2.串行执行

- (void)test2

{

    // 同步函数 + 并发队列

    

    dispatch_sync(dispatch_get_global_queue(0, 0), ^{

        

        NSLog(@"耗时操作1,%@",[NSThread currentThread]);

    });

    dispatch_sync(dispatch_get_global_queue(0, 0), ^{

        

        NSLog(@"耗时操作2,%@",[NSThread currentThread]);

    });

    dispatch_sync(dispatch_get_global_queue(0, 0), ^{

        

        NSLog(@"耗时操作3,%@",[NSThread currentThread]);

    });

}

 

 

// 同步函数+串行队列

// 结论:1.主线程执行/没有开启新线程

//       2. 串行执行

- (void)test1

{

    // 创建串行队列

    dispatch_queue_t serialQueue = dispatch_queue_create("itcast", DISPATCH_QUEUE_SERIAL);

    

    // 同步函数+串行队列

    // 结论:1.主线程执行/没有开启新线程

    //       2. 串行执行

    dispatch_sync(serialQueue, ^{

        NSLog(@"耗时操作1,%@",[NSThread currentThread]);

    });

    

    dispatch_sync(serialQueue, ^{

        NSLog(@"耗时操作2,%@",[NSThread currentThread]);

    });

    

    dispatch_sync(serialQueue, ^{

        NSLog(@"耗时操作3,%@",[NSThread currentThread]);

    });

}

 

@end

同步函数

标签:

原文地址:http://www.cnblogs.com/R-X-L/p/4777561.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!