Dispatch_apply, which can be iterated iteratively, can be improved in performance, provided the loop does not care about the order of the iterations
dispatch_queue_t queue = dispatch_queue_create ("Hjiang", dispatch_queue_concurrent); Dispatch_async (Queue, ^{dispatch_apply (ten, queue, ^ (size_t i) {NSLog (@ "%zd%@", I,[nsthread Currentthre AD]); }); }); NSLog (@ "Fun end ..."); Results: 1. This method iterates over the new thread that contains the current thread, which will block the current thread, and if it is currently in the main thread, it may affect user Click event Handling/Operation 2. Only after the iteration task is complete, the post-iteration task can be executed 2017-09-02 21:13:31.709 GCD test [16672:349258] fun end ... 2017-09-02 21:13:31.709 GCD Test [16672:349297] 0 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21 : 13:31.709 GCD Test [16672:349297] 1 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.709 GC D Test [16672:349297] 2 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.709 GCD test [16,672:349 297] 3 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test [16672:349297] 4 <NS Thread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test[16672:349297] 5 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test [16,672:349,297 ] 6 <nsthread:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test [16672:349297] 7 <nsthr Ead:0x608000260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test [16672:349297] 8 <nsthread:0x608000 260000>{number = 3, name = (null)} 2017-09-02 21:13:31.710 GCD test [16672:349297] 9 <nsthread:0x608000260000>{nu Mber = 3, name = (NULL)}
If you do not want the current thread to participate in the iteration task, you can include the entire iteration task in a new line approached ( this is preferred ), such as:
dispatch_queue_t queue = dispatch_queue_create ("Hjiang", dispatch_queue_concurrent); Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{dispatch_apply (ten, queue, ^ (size_t i) {NSLog (@ "%zd%@", I,[nsthread CurrentThread]); }); }); NSLog (@ "Fun end ..."); 2017-09-02 21:18:32.140 GCD Test [16774:354627] fun end ... 2017-09-02 21:18:32.141 GCD Test [16774:354715] 1 <nsthread:0x600000078380>{number = 4, name = (null)} 2017-09-02 21 : 18:32.141 GCD Test [16774:354716] 0 <nsthread:0x60800006b740>{number = 3, name = (null)} 2017-09-02 21:18:32.141 GC D Test [16774:354737] 3 <nsthread:0x600000078600>{number = 6, name = (null)} 2017-09-02 21:18:32.141 GCD test [16,774:354 718] 2 <nsthread:0x608000074c40>{number = 5, name = (null)} 2017-09-02 21:18:32.141 GCD test [16774:354715] 4 <NS Thread:0x600000078380>{number = 4, name = (null)} 2017-09-02 21:18:32.141 GCD test [16774:354716] 5 <nsthread:0x60800006b740>{number = 3, name = (null)} 2017-09-02 21:18:32.142 GCD test [16774:354737] 6 <NSTHREAD:0X600000078600&G T {Number = 6, name = (null)} 2017-09-02 21:18:32.142 GCD test [16774:354718] 7 <nsthread:0x608000074c40>{number = 5, name = (null)} 2017-09-02 21:18:32.142 GCD test [16774:354715] 8 <nsthread:0x600000078380>{number = 4, name = (NULL) } 2017-09-02 21:18:32.142 GCD test [16774:354716] 9 <nsthread:0x60800006b740>{number = 3, name = (NULL)}
However, this approach needs to be aware of the problem that the newly-opened thread contains the iteration task, not the same queue, or the entire iteration task executes in the same thread, which is equivalent to the iteration task being executed serially, such as:
dispatch_queue_t queue = dispatch_queue_create ("Hjiang", dispatch_queue_concurrent); Dispatch_async (Queue, ^{dispatch_apply (ten, queue, ^ (size_t i) {NSLog (@ "%zd%@", I,[nsthread Currentthre AD]); }); }); NSLog (@ "Fun end ..."); 2017-09-02 21:20:03.237 GCD Test [16813:356507] 0 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21 : 20:03.237 GCD Test [16813:356407] fun end ... 2017-09-02 21:20:03.237 GCD Test [16813:356507] 1 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21 : 20:03.237 GCD Test [16813:356507] 2 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.238 GC D Test [16813:356507] 3 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD test [16,813:356 507] 4 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD test [16813:356507] 5 <NS Thread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD Test [16813:356507] 6 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.238 GCD Test [16813:356507] 7 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.239 GCD test [16,813:3,565 [] 8 <nsthread:0x600000272900>{number = 3, name = (null)} 2017-09-02 21:20:03.239 GCD test [16813:356507] 9 <nst Hread:0x600000272900>{number = 3, name = (NULL)}
GCD dispatch_apply Basic Use