GCD dispatch_apply Basic Use

Source: Internet
Author: User
Tags gcd

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 &LT;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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.