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