当前位置:编程学习 > wap >>

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

1 前言

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


2 代码实例
ZYAppDelegate.m

 

[plain]
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 

    //创建自己命名的队列 
    dispatch_queue_t firstSerialQueue = dispatch_queue_create("ZYAppDelegate.易做图.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.易做图.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 易做图QueueTest2[1030:1303] First iteration, counter = 0

2013-05-12 11:41:59.223 易做图QueueTest2[1030:1303] First iteration, counter = 1

2013-05-12 11:41:59.223 易做图QueueTest2[1030:1303] First iteration, counter = 2

2013-05-12 11:41:59.224 易做图QueueTest2[1030:1303] First iteration, counter = 3

2013-05-12 11:41:59.225 易做图QueueTest2[1030:1303] First iteration, counter = 4

2013-05-12 11:41:59.226 易做图QueueTest2[1030:1303] Second iteration, counter = 0

2013-05-12 11:41:59.227 易做图QueueTest2[1030:1303] Second iteration, counter = 1

2013-05-12 11:41:59.228 易做图QueueTest2[1030:1303] Second iteration, counter = 2

2013-05-12 11:41:59.229 易做图QueueTest2[1030:1303] Second iteration, counter = 3

2013-05-12 11:41:59.230 易做图QueueTest2[1030:1303] Second iteration, counter = 4

2013-05-12 11:41:59.232 易做图QueueTest2[1030:1303] Third iteration, counter = 0

2013-05-12 11:41:59.232 易做图QueueTest2[1030:1303] Third iteration, counter = 1

2013-05-12 11:41:59.233 易做图QueueTest2[1030:1303] Third iteration, counter = 2

2013-05-12 11:41:59.233 易做图QueueTest2[1030:1303] Third iteration, counter = 3

2013-05-12 11:41:59.234 易做图QueueTest2[1030:1303] Third iteration, counter = 4

 

补充:移动开发 , IOS ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,