你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> IOS開發(66)之構建自己的分派隊列

IOS開發(66)之構建自己的分派隊列

編輯:IOS開發綜合

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

 

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved