dispatch_queue_concurrent 深碍√TFBOYSˉ_ 2022-06-08 02:54 170阅读 0赞 # 并发队列 # ## 特点 ## * 以`先进先出`的方式,`并发`调度队列中的任务执行 * 如果当前调度的任务是`同步`执行的,会等待任务执行完成后,再调度后续的任务 * 如果当前调度的任务是`异步`执行的,同时底层线程池有可用的线程资源,会再新的线程调度后续任务的执行 ## 队列创建 ## dispatch_queue_t queue = dispatch_queue_create("com.itheima.queue", DISPATCH_QUEUE_CONCURRENT); 1 ## 并发队列演练 ## * 并发队列 异步执行 /** 提问:是否开线程?是否顺序执行?come here 的位置? */ - (void)gcdDemo3 { // 1. 队列 dispatch_queue_t q = dispatch_queue_create("itheima", DISPATCH_QUEUE_CONCURRENT); // 2. 执行任务 for (int i = 0; i < 10; ++i) { dispatch_async(q, ^{ NSLog(@"%@ - %d", [NSThread currentThread], i); }); } NSLog(@"come here"); } 2017-09-12 19:22:13.785046+0800 MovieCaptureDemo[777:119224] [DYMTLInitPlatform] platform initialization successful 2017-09-12 19:22:13.926596+0800 MovieCaptureDemo[777:119161] Unknown class 录制 in Interface Builder file. 2017-09-12 19:22:13.954695+0800 MovieCaptureDemo[777:119161] come here 2017-09-12 19:22:13.956958+0800 MovieCaptureDemo[777:119230] <NSThread: 0x170065b80>{number = 4, name = (null)} - 0 2017-09-12 19:22:13.957041+0800 MovieCaptureDemo[777:119230] <NSThread: 0x170065b80>{number = 4, name = (null)} - 1 2017-09-12 19:22:13.957088+0800 MovieCaptureDemo[777:119230] <NSThread: 0x170065b80>{number = 4, name = (null)} - 2 2017-09-12 19:22:13.957182+0800 MovieCaptureDemo[777:119215] <NSThread: 0x1700782c0>{number = 5, name = (null)} - 4 2017-09-12 19:22:13.957248+0800 MovieCaptureDemo[777:119215] <NSThread: 0x1700782c0>{number = 5, name = (null)} - 5 2017-09-12 19:22:13.957272+0800 MovieCaptureDemo[777:119230] <NSThread: 0x170065b80>{number = 4, name = (null)} - 3 2017-09-12 19:22:13.957324+0800 MovieCaptureDemo[777:119215] <NSThread: 0x1700782c0>{number = 5, name = (null)} - 6 2017-09-12 19:22:13.957373+0800 MovieCaptureDemo[777:119215] <NSThread: 0x1700782c0>{number = 5, name = (null)} - 8 2017-09-12 19:22:13.957430+0800 MovieCaptureDemo[777:119230] <NSThread: 0x170065b80>{number = 4, name = (null)} - 7 2017-09-12 19:22:13.957444+0800 MovieCaptureDemo[777:119215] <NSThread: 0x1700782c0>{number = 5, name = (null)} - 9 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 * 并发队列 同步执行 /** 提问:是否开线程?是否顺序执行?come here 的位置? */ - (void)gcdDemo4 { // 1. 队列 dispatch_queue_t q = dispatch_queue_create("itheima", DISPATCH_QUEUE_CONCURRENT); // 2. 执行任务 for (int i = 0; i < 10; ++i) { dispatch_sync(q, ^{ NSLog(@"%@ - %d", [NSThread currentThread], i); }); NSLog(@"---> %i", i); } NSLog(@"come here"); } 2017-09-12 19:17:15.100959+0800 MovieCaptureDemo[765:117979] [DYMTLInitPlatform] platform initialization successful 2017-09-12 19:17:15.224936+0800 MovieCaptureDemo[765:117941] Unknown class 录制 in Interface Builder file. 2017-09-12 19:17:15.249309+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 0 2017-09-12 19:17:15.249349+0800 MovieCaptureDemo[765:117941] ---> 0 2017-09-12 19:17:15.249391+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 1 2017-09-12 19:17:15.249405+0800 MovieCaptureDemo[765:117941] ---> 1 2017-09-12 19:17:15.249434+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 2 2017-09-12 19:17:15.249447+0800 MovieCaptureDemo[765:117941] ---> 2 2017-09-12 19:17:15.249474+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 3 2017-09-12 19:17:15.249487+0800 MovieCaptureDemo[765:117941] ---> 3 2017-09-12 19:17:15.249514+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 4 2017-09-12 19:17:15.249526+0800 MovieCaptureDemo[765:117941] ---> 4 2017-09-12 19:17:15.249553+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 5 2017-09-12 19:17:15.249627+0800 MovieCaptureDemo[765:117941] ---> 5 2017-09-12 19:17:15.249656+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 6 2017-09-12 19:17:15.249668+0800 MovieCaptureDemo[765:117941] ---> 6 2017-09-12 19:17:15.249695+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 7 2017-09-12 19:17:15.249708+0800 MovieCaptureDemo[765:117941] ---> 7 2017-09-12 19:17:15.249735+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 8 2017-09-12 19:17:15.249747+0800 MovieCaptureDemo[765:117941] ---> 8 2017-09-12 19:17:15.249774+0800 MovieCaptureDemo[765:117941] <NSThread: 0x174261900>{number = 1, name = main} - 9 2017-09-12 19:17:15.249787+0800 MovieCaptureDemo[765:117941] ---> 9 2017-09-12 19:17:15.249796+0800 MovieCaptureDemo[765:117941] come here
还没有评论,来说两句吧...