GCD Common methods

Source: Internet
Author: User

1. Deferred operation

2. Disposable code

3. Queue Groups

/** * Deferred execution Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0 * nsec_per_sec)), Dispatch_get_main_queue (  ), ^{///2 seconds after executing the code here Asynchronously ...});  Deferred execution: No more writing method is required, and it also passes a queue, and we can specify and schedule its threads. If the queue is a home column, it is executed on the main thread, and if the queue is a concurrent queue, a new thread is opened and executed in the child thread. */-(void) test1 {NSLog (@"print the current thread---%@", [Nsthread CurrentThread]); //deferred execution, the second way//can schedule its thread (1), the home rowdispatch_queue_t queue=Dispatch_get_main_queue (); Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5.0* nsec_per_sec)), queue, ^{NSLog (@"Home Row-deferred execution------%@", [Nsthread CurrentThread]);             }); //can schedule its threads (2), concurrent queues//1. Get Global concurrent Queuesdispatch_queue_t queue1= Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. Calculate time for task executiondispatch_time_t When=dispatch_time (Dispatch_time_now, (int64_t) (5.0*nsec_per_sec)); //3. The task in the queue will be executed at this point in timeDispatch_after (when, queue1, ^{NSLog (@"Concurrent queues-deferred execution------%@", [Nsthread CurrentThread]); });}
/* * *  2. Using  the Dispatch_once one-time code using the Dispatch_once function can ensure that a piece of code in the process of operation is executed only 1  static dispatch_once_t Oncetoken;  Dispatch_once (&oncetoken, ^{  //Only executes 1 times of code (which is thread-safe by default)  });   */-(void) test2    {static  dispatch_once_t Oncetoken;         Dispatch_once (&oncetoken, ^{                 NSLog (@ ) The line code executes only once "  );             });}
/** Queue Group*/- (void) test3 {dispatch_group_t Group=dispatch_group_create (); Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{                //perform 1 time-consuming asynchronous operations            }); Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{                //perform 1 time-consuming asynchronous operations            }); Dispatch_group_notify (Group, Dispatch_get_main_queue (),^{                //when the previous asynchronous operation is complete, return to the main thread ... (Result processing)            });}

GCD Common methods

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.