首页 > 其他 > 详细

同步函数

时间:2015-09-02 08:12:14      阅读:274      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!