- ( void)test1 { // 创建一个并发队列 // 并发队列+异步任务:创建多个线程,并发执行 dispatch_queue_t queue = dispatch_queue_create( " tqh.com ", DISPATCH_QUEUE_CONCURRENT); // 一步创建一个任务,任务不会立即执行 dispatch_async(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // number = 4, name = (null) 无序 } - ( void)test2 { // 并发队列+同步任务:不会开启新线程,在父线程中同步执行各个子线程,也就是逐一执行,并且是添加过任务后立即执行,之后才能添加下一个任务 dispatch_queue_t queue = dispatch_queue_create( " tqh.com ", DISPATCH_QUEUE_CONCURRENT); dispatch_sync(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // number = 1, name = main 有序 } - ( void)test3 { // 串行队列+同步任务:不会开启新线程,在父线程中同步执行各个子线程,也就是逐一执行,并且是添加过任务后立即执行,之后才能添加下一个任务 dispatch_queue_t queue = dispatch_queue_create( " tqh.com ", DISPATCH_QUEUE_SERIAL); dispatch_sync(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // number = 1, name = main 有序 } - ( void)test4 { // 串行队列+异步任务:创建新线程,但是只会创建一个新线程,所有的任务都是在这个子线程里执行,执行顺序按照添加任务 的先后顺序,并且不是立即执行,而是等整个方法 dispatch_queue_t queue = dispatch_queue_create( " tqh.com ", DISPATCH_QUEUE_SERIAL); dispatch_async(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // number = 2,name = (null),创建了一个线程 有序 } - ( void)test5 { // 主队列+异步任务:不会创建新线程,所有的任务都是在这个父线程里执行,执行顺序按照添加任务 的先后顺序,并且不是立即执行,而是等整个方法结束后依次执行 dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_async(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_async(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // number = 1, name = main 有序 } - ( void)test6 { // 这个会产生问题,死锁,添加任务到主队列的任务要求立即执行,但是主队列是串行队列,当前任务要求执行完当前任务在执行新添加的任务。结果就是:两个任务互相等待,产生死锁 dispatch_queue_t queue = dispatch_get_main_queue(); dispatch_sync(queue, ^{ NSLog( @" 1--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 2--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 3--%@-- ",[NSThread currentThread]); }); dispatch_sync(queue, ^{ NSLog( @" 4--%@-- ",[NSThread currentThread]); }); NSLog( @" 结束 "); // 没有打印信息 } // 延时执行:没有NSObject和NsTimer精确度高 - ( void)test7 { NSLog( @" %@ ",[NSDate date]); dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( 2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ NSLog( @" %@ ",[NSDate date]); }); } // 一次性代码 - ( void)test8 { for ( int i = 0; i < 10; i ++) { [self onece]; } // 只走了第一次 } - ( void)onece { static dispatch_once_t once; dispatch_once(&once, ^{ NSLog( @" 只走一次%@ ",[NSThread currentThread]); }); NSLog( @" ---------------- "); } // 快速迭代,顺序不稳定 - ( void)test9 { dispatch_apply( 10, dispatch_get_global_queue( 0, 0), ^(size_t index) { NSLog( @" %ld %@ ",index,[NSThread currentThread]); }); // 无序 } // 队列组 - ( void)test10 { dispatch_group_t group = dispatch_group_create(); dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); // 先执行3个耗时操作 dispatch_group_async(group, queue, ^{ for ( int i = 0 ; i < 1000; i ++) { NSLog( @" 1--%@-- ",[NSThread currentThread]); } }); dispatch_group_async(group, queue, ^{ for ( int i = 0 ; i < 1000; i ++) { NSLog( @" 2--%@-- ",[NSThread currentThread]); } }); dispatch_group_async(group, queue, ^{ for ( int i = 0 ; i < 1000; i ++) { NSLog( @" 3--%@-- ",[NSThread currentThread]); } }); // 等到以上任务完成后才会执行这个notify任务 dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog( @" 最后执行main--%@-- ",[NSThread currentThread]); }); }