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

block用法总结(随时更新)

时间:2015-03-31 19:36:11      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:

dispatch_queue_t :

创建方法:

dispatch_queue_t queue = dispatch_queue_create("myblockqueue",DISPATCH_QUEUE_CONCURRENT);
DISPATCH_QUEUE_CONCURRENT //并发
DISPATCH_QUEUE_SERIAL     //串行

串行的队列也就是添加进去的一个一个block是按顺序执行,比如:

    dispatch_queue_t queue = dispatch_queue_create("myblockqueue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_async(queue, ^{
        sleep(3);
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
    });

结果是:

1

2

而并发的队列是:

    dispatch_queue_t queue = dispatch_queue_create("myblockqueue", DISPATCH_QUEUE_CONCURRENT);
    
    dispatch_async(queue, ^{
        sleep(3);
        NSLog(@"1");
    });
    dispatch_async(queue, ^{
        NSLog(@"2");
    });

结果是

2

1

还有三种队列是系统定义的队列,根据优先级别不同有3种:


  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); //获取系统的queue

#define DISPATCH_QUEUE_PRIORITY_HIGH 2
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0
#define DISPATCH_QUEUE_PRIORITY_LOW (-2)

当然,也可以获取主线程队列

 

block用法总结(随时更新)

标签:

原文地址:http://www.cnblogs.com/wangyutao/p/4381621.html

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