PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!
1 前言
应用 dispatch_queue_create 函数。 创立你自己的、奇特命名的分派队列。
2 代码实例
ZYAppDelegate.m
- (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
3 结语
以上是所有内容,希望对大家有所帮助。
Demo下载地址:
文章结束给大家分享下程序员的一些笑话语录: 警告
有一个小伙子在一个办公大楼的门口抽着烟,一个妇女路过他身边,并对他 说, “你知道不知道这个东西会危害你的健康?我是说, 你有没有注意到香烟 盒上的那个警告(Warning)?” 小伙子说,“没事儿,我是一个程序员”。 那妇女说,“这又怎样?” 程序员说,“我们从来不关心 Warning,只关心 Error”