当前位置: 移动技术网 > IT编程>移动开发>IOS > IOS开发(66)之构建自己的分派队列

IOS开发(66)之构建自己的分派队列

2019年05月12日  | 移动技术网IT编程  | 我要评论

男生短碎发型,谷智鑫女友汤晶媚,年度美酒怎么做

1 前言

使用 dispatch_queue_create 函数。 创建你自己的、独特命名的分派队列。


2 代码实例
zyappdelegate.m

 

[plain]
- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions 

    //创建自己命名的队列 
    dispatch_queue_t firstserialqueue = dispatch_queue_create("zyappdelegate.gcd.serialqueue1", 0); 
    //执行block 
    dispatch_async(firstserialqueue, ^{ 
        nsuinteger counter = 0; 
        for (counter = 0;counter < 5;counter++){ 
            nslog(@"first iteration, counter = %lu", (unsigned long)counter); 
        } }); 
    dispatch_async(firstserialqueue, ^{ 
        nsuinteger counter = 0; 
        for (counter = 0; 
             counter < 5; 
             counter++){ 
            nslog(@"second iteration, counter = %lu", (unsigned long)counter); 
        } }); 
    dispatch_async(firstserialqueue, ^{ 
        nsuinteger counter = 0; 
        for (counter = 0; 
             counter < 5; 
             counter++){ 
            nslog(@"third iteration, counter = %lu", (unsigned long)counter); 
        } }); 
    //释放队列 
    dispatch_release(firstserialqueue); 
    self.window = [[[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease]; 
    // override point for customization after application launch. 
    self.viewcontroller = [[[zyviewcontroller alloc] initwithnibname:@"zyviewcontroller" bundle:nil] autorelease]; 
    self.window.rootviewcontroller = self.viewcontroller; 
    [self.window makekeyandvisible]; 
    return yes; 

- (bool)application:(uiapplication *)application didfinishlaunchingwithoptions:(nsdictionary *)launchoptions
{
    //创建自己命名的队列
    dispatch_queue_t firstserialqueue = dispatch_queue_create("zyappdelegate.gcd.serialqueue1", 0);
    //执行block
    dispatch_async(firstserialqueue, ^{
        nsuinteger counter = 0;
        for (counter = 0;counter < 5;counter++){
            nslog(@"first iteration, counter = %lu", (unsigned long)counter);
        } });
    dispatch_async(firstserialqueue, ^{
        nsuinteger counter = 0;
        for (counter = 0;
             counter < 5;
             counter++){
            nslog(@"second iteration, counter = %lu", (unsigned long)counter);
        } });
    dispatch_async(firstserialqueue, ^{
        nsuinteger counter = 0;
        for (counter = 0;
             counter < 5;
             counter++){
            nslog(@"third iteration, counter = %lu", (unsigned long)counter);
        } });
    //释放队列
    dispatch_release(firstserialqueue);
    self.window = [[[uiwindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease];
    // override point for customization after application launch.
    self.viewcontroller = [[[zyviewcontroller alloc] initwithnibname:@"zyviewcontroller" bundle:nil] autorelease];
    self.window.rootviewcontroller = self.viewcontroller;
    [self.window makekeyandvisible];
    return yes;
}
运行后控制台结果


2013-05-12 11:41:59.221 gcdqueuetest2[1030:1303] first iteration, counter = 0

2013-05-12 11:41:59.223 gcdqueuetest2[1030:1303] first iteration, counter = 1

2013-05-12 11:41:59.223 gcdqueuetest2[1030:1303] first iteration, counter = 2

2013-05-12 11:41:59.224 gcdqueuetest2[1030:1303] first iteration, counter = 3

2013-05-12 11:41:59.225 gcdqueuetest2[1030:1303] first iteration, counter = 4

2013-05-12 11:41:59.226 gcdqueuetest2[1030:1303] second iteration, counter = 0

2013-05-12 11:41:59.227 gcdqueuetest2[1030:1303] second iteration, counter = 1

2013-05-12 11:41:59.228 gcdqueuetest2[1030:1303] second iteration, counter = 2

2013-05-12 11:41:59.229 gcdqueuetest2[1030:1303] second iteration, counter = 3

2013-05-12 11:41:59.230 gcdqueuetest2[1030:1303] second iteration, counter = 4

2013-05-12 11:41:59.232 gcdqueuetest2[1030:1303] third iteration, counter = 0

2013-05-12 11:41:59.232 gcdqueuetest2[1030:1303] third iteration, counter = 1

2013-05-12 11:41:59.233 gcdqueuetest2[1030:1303] third iteration, counter = 2

2013-05-12 11:41:59.233 gcdqueuetest2[1030:1303] third iteration, counter = 3

2013-05-12 11:41:59.234 gcdqueuetest2[1030:1303] third iteration, counter = 4

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网