笛里谁知壮士心,沙头空照征人骨。这篇文章主要讲述GCD dispatch_apply基本使用相关的知识,希望能为你提供帮助。
dispatch_apply,可以并发的循环迭代,性能上可以提高,前提是循环不在乎迭代的顺序
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ dispatch_apply(10, queue, ^(size_t i) { NSLog(@"%zd %@",i,[NSThread currentThread]); }); }); NSLog(@"fun end..."); 结果:1.此方式迭代的新开的线程含有当前线程,也就是会阻塞当前线程,如果当前是在主线程的话可能会影响用户点击事件处理/操作 2.只有迭代任务完成后,迭代的后的任务才能被执行 2017-09-02 21:13:31.709 GCD测试[16672:349258] fun end... 2017-09-02 21:13:31.709 GCD测试[16672:349297] 0 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.709 GCD测试[16672:349297] 1 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.709 GCD测试[16672:349297] 2 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.709 GCD测试[16672:349297] 3 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 4 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 5 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 6 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 7 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 8 < NSThread: 0x608000260000> {number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD测试[16672:349297] 9 < NSThread: 0x608000260000> {number = 3, name = (null)}
如果不想让在当前线程参与迭代任务,可以把整个迭代任务包含在一个新的线程中 (优先采用此方式), 如:
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT); dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ dispatch_apply(10, queue, ^(size_t i) { NSLog(@"%zd %@",i,[NSThread currentThread]); }); }); NSLog(@"fun end..."); 2017-09-02 21:18:32.140 GCD测试[16774:354627] fun end... 2017-09-02 21:18:32.141 GCD测试[16774:354715] 1 < NSThread: 0x600000078380> {number = 4, name = (null)} 2017-09-02 21:18:32.141 GCD测试[16774:354716] 0 < NSThread: 0x60800006b740> {number = 3, name = (null)} 2017-09-02 21:18:32.141 GCD测试[16774:354737] 3 < NSThread: 0x600000078600> {number = 6, name = (null)} 2017-09-02 21:18:32.141 GCD测试[16774:354718] 2 < NSThread: 0x608000074c40> {number = 5, name = (null)} 2017-09-02 21:18:32.141 GCD测试[16774:354715] 4 < NSThread: 0x600000078380> {number = 4, name = (null)} 2017-09-02 21:18:32.141 GCD测试[16774:354716] 5 < NSThread: 0x60800006b740> {number = 3, name = (null)} 2017-09-02 21:18:32.142 GCD测试[16774:354737] 6 < NSThread: 0x600000078600> {number = 6, name = (null)} 2017-09-02 21:18:32.142 GCD测试[16774:354718] 7 < NSThread: 0x608000074c40> {number = 5, name = (null)} 2017-09-02 21:18:32.142 GCD测试[16774:354715] 8 < NSThread: 0x600000078380> {number = 4, name = (null)} 2017-09-02 21:18:32.142 GCD测试[16774:354716] 9 < NSThread: 0x60800006b740> {number = 3, name = (null)}
【GCD dispatch_apply基本使用】
但此方式需要注意一个问题,就是新开的线程包含的迭代任务,不要是同一个队列,要不然整个迭代任务会在同一个线程中执行,相当于迭代任务串行执行,如:
dispatch_queue_t queue = dispatch_queue_create("HJiang", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ dispatch_apply(10, queue, ^(size_t i) { NSLog(@"%zd %@",i,[NSThread currentThread]); }); }); NSLog(@"fun end..."); 2017-09-02 21:20:03.237 GCD测试[16813:356507] 0 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.237 GCD测试[16813:356407] fun end... 2017-09-02 21:20:03.237 GCD测试[16813:356507] 1 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.237 GCD测试[16813:356507] 2 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD测试[16813:356507] 3 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD测试[16813:356507] 4 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD测试[16813:356507] 5 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD测试[16813:356507] 6 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD测试[16813:356507] 7 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.239 GCD测试[16813:356507] 8 < NSThread: 0x600000272900> {number = 3, name = (null)} 2017-09-02 21:20:03.239 GCD测试[16813:356507] 9 < NSThread: 0x600000272900> {number = 3, name = (null)}
推荐阅读
- Win8系统虚拟内存设置办法
- Android 7.0Power 按键处理流程
- android ——多线程编程
- Android开发之动态设置字体的样式和粗细
- POJ 2385 -- Apple Catching
- 解决Android中,禁止ScrollView内的控件改变之后自动滚动 - 转
- Android——Activity以及Service的启动方式
- 钟情平台化的锁屏赚钱APP如何重塑现象级市场()
- xamarin.android 绑定百度地图SDK遇到的问题