Several common methods used in the GCD of ios-thread GCD---

Source: Internet
Author: User
Tags gcd

Several common methods in GCD

1. The code is only executed once: (In this way, you can simply create a singleton object)

Method Dispatch_once (&,^ (void) {});

1- (void) Testone2 3 {4 5     Staticdispatch_once_t Oncetoken;6 7Dispatch_once (&oncetoken,^{8 9NSLog (@"the code block executes only once");Ten  One     }); A  -}

2. A piece of code is executed between two threads, and the threads are detached, without having them execute concurrently

Method: Dispatch_barrier_async (queue,^ (void) {});

1- (void) Testbarrier2 {3     //queue cannot be a parallel queue for a system4dispatch_queue_t Concurrentqueue = Dispatch_queue_create ("Concurrentqueue", dispatch_queue_concurrent);5     //Thread One6Dispatch_async (Concurrentqueue, ^{7          for(intI=0; i< -; i++) {8NSLog (@"line Cheng:%d", i);9         }Ten     }); One     //thread Two ADispatch_async (Concurrentqueue, ^{ -          for(intI=0; i< -; i++) { -NSLog (@"thread Two:%d", i); the         } -     }); -     //online Cheng, after thread two execution completes, execute a piece of code before the threads are executed, and then start the execution line Cheng -Dispatch_barrier_async (Concurrentqueue, ^{ +NSLog (@"Barrier"); -     }); +      A     //Line Cheng atDispatch_async (Concurrentqueue, ^{ -          for(intI=0; i< -; i++) { -NSLog (@"thread Three:%d", i); -         } -     }); -     //thread Four inDispatch_async (Concurrentqueue, ^{ -          for(intI=0; i< -; i++) { toNSLog (@"thread Four:%d", i); +         } -     }); the}

3. Execute a piece of code for this

Method: Dispatch_apply (size_t t,queue,^ (size_t times) {});

The parameter times in a block refer to the current number of threads in the queue, such as: The method executes 10 times, creates 10 threads in the queue, and executes basically simultaneously, times identifies the current number of threads (so times are printed out of order).

1- (void) testapply2 {3     /*4 number of times @param1 code block was executed5 @param2 the queue where the code block resides6 @param3 the execution body of a thread7      */8size_t T =Ten;9dispatch_queue_t globalqueue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);Ten      OneDispatch_apply (t, Globalqueue, ^(size_t q) { ANSLog (@"thread Cheng:%ld", q); -     }); -}

4. The code executes after a period of time

Method: Dispatch_after (dispatch_time_t time,queue,^ (void) {}),---The time parameter is the number of seconds to execute code in block

1- (void) Testaffter2 {3NSLog (@"before executing");4     //Current time 10 seconds later5dispatch_time_t time = Dispatch_time (Dispatch_time_now, Nsec_per_sec *Ten);6     /*7 @param1: Time8 @param2: The queue where the thread is located9 @param3: The execution body of a threadTen      */ OneDispatch_after (Time, Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{ ANSLog (@"executed after"); -     }); -}

The 5.GCD also provides the concept of a group that can put all the queues (threads in the queue) into groups

Dispatch_group_notify (dispatch_group_t group,queue,^ (void) {}) is executed when the threads in all queues in the group have finished executing;

Function: When a page needs more than one download, it is possible to add all the threads that have been asynchronously downloaded to the queue and then add them to the group, when all the downloads have been completed, and then the page is refreshed in the Dispatch_group_notify method.

1 //put the thread in a group2- (void) Testgroup3 {4     //Creating a group5dispatch_group_t Group =dispatch_group_create ();6     7dispatch_queue_t globalqueue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);8     /*9 @param1: The group where the thread residesTen @param2: The queue where the thread is located One @param3: The execution body of a thread A      */ -Dispatch_group_async (Group, globalqueue, ^{ -          for(intI=0; i< -; i++) { theNSLog (@"line Cheng:%d", i); -         } -     }); -     //adding a thread in a group +Dispatch_group_async (Group, globalqueue, ^{ -          for(intI=0; i< -; i++) { +NSLog (@"thread Two:%d", i); A         } at     }); -      -     //code that is called after all threads in a multi-thread group have finished executing -Dispatch_group_notify (Group, globalqueue, ^{ -NSLog (@"thread all method execution complete"); -     }); in}

GCD is so powerful, there's always one for you.

Several common methods used in the GCD of ios-thread GCD---

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.