欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2022-06-07 14:57:35
1 前言 使用 dispatch_queue_create 函数。 创建你自己的、独特命名的分派队列。 2 代码实例 zyappdelegate.m   [...

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