//采用系统的方式添加串行的任务 - (IBAction)SerialMainAction:(UIButton *)sender { // 系统提供的串行队列其实就是主线程队列 // 1拿到主线程 dispatch_queue_t main = dispatch_get_main_queue(); // 2给主线程队列添加任务 dispatch_async(main, ^{ NSLog(@"omg%@",[NSThread currentThread]); }); dispatch_async(main, ^{ NSLog(@"two%@",[NSThread currentThread]); }); dispatch_async(main, ^{ NSLog(@"three%@",[NSThread currentThread]); }); dispatch_async(main, ^{ NSLog(@"four%@",[NSThread currentThread]); }); dispatch_async(main, ^{ NSLog(@"five%@",[NSThread currentThread]); }); }
上面的是串行队列,上面也说了,串行队列其实就是主线程队列,添加的几个任务是为了看出执行顺序
下面的是自定义的方式添加串行任务,里面的几个任务也是为了查看执行顺序
//采用自定义的方式添加串行任务 - (IBAction)SerialBySelfAction:(UIButton *)sender { //创建串行队列 dispatch_queue_t serialQueue = dispatch_queue_create("yan5", DISPATCH_QUEUE_SERIAL); // 2添加任务 dispatch_async(serialQueue, ^{ NSLog(@"one%@",[NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"two%@",[NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"three%@",[NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"four%@",[NSThread currentThread]); }); dispatch_async(serialQueue, ^{ NSLog(@"five%@",[NSThread currentThread]); }); }
原文:http://www.cnblogs.com/guzhen/p/5107796.html