Serial, parallel, group, once, obstacle, delay, repeat (GCD) in iOS

Source: Internet
Author: User

#import"OneViewController.h"@interface Oneviewcontroller () @end @implementation Oneviewcontroller- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.}- (void) didreceivememorywarning {[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}#pragmaMark method//rely on C language to execute, the most efficient//Serial Queue-(ibaction) SERIALBTN: (ID) Sender {//1. Create row-to-column//1. Get the system to create a serial pair of columns, in the main thread is to implement thread synchronizationdispatch_queue_t queue1 =Dispatch_get_main_queue (); //2. Create a serial queue on your own, and the task implements thread synchronization in the child threadsdispatch_queue_t queue2 = Dispatch_queue_create ("Com.lanou3g.oa", dispatch_queue_serial);//string-The function of the uniquely marked (Anti-domain form), dispatch_queue_serial--specified as serial//2. Adding tasks to the queueDispatch_async (queue2, ^{NSLog (@"Task one:%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue2,^{NSLog (@"task two:%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue2,^{NSLog (@"task three:%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue2,^{NSLog (@"task four:%@", [Nsthread CurrentThread]);        }); //Releasedispatch_release (queue2); }//Parallel (concurrent) Queues-(ibaction) PARALLELBTN: (ID) Sender {//1. Create the concurrent queue, the system created four//1. Create a good concurrent queue using the systemdispatch_queue_t queue1 = Dispatch_get_global_queue (Dispatch_queue_priority_default,0);//parameters are: priority, reserved parameters//2. Create a concurrent queue yourselfdispatch_queue_t queue2 = Dispatch_queue_create ("Com.lanou3g.oa", dispatch_queue_concurrent);//parameter 1. Unique indication, parameter two specified as concurrent queue//3. Adding tasks to the queueDispatch_async (queue2, ^{NSLog (@"Task 1:%@", [Nsthread CurrentThread]);        }); Dispatch_async (Queue2,^{NSLog (@"Task 2:%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue2,^{NSLog (@"Task 3:%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue2,^{NSLog (@"Task 4:%@", [Nsthread CurrentThread]);    }); //Request Picture//skip to main thread refresh interfaceDispatch_async (Dispatch_get_main_queue (), ^{    //update UI Display picture        }); //Releasedispatch_release (queue2); }//Grouping Tasks-(ibaction) GROUPBTN: (ID) Sender {//1. Create a parallel queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2. Create a groupdispatch_group_t Group =dispatch_group_create (); //3. Add a task in a party groupDispatch_group_async (group, queue, ^{NSLog (@"task one, request 0-20 m data");    }); Dispatch_group_async (group, queue,^{NSLog (@"task two, request 0-20 m data");    }); Dispatch_group_async (group, queue,^{NSLog (@"task three, request 0-20 m data");    }); Dispatch_group_async (group, queue,^{NSLog (@"task Four, request 0-20 m data");    }); Dispatch_group_async (group, queue,^{NSLog (@"task Five, request 0-20 m data");    }); //4. When all tasks within the group are completed, the stitching operationDispatch_group_notify (group, queue, ^{NSLog (@"Splice Data!");    }); //Releasedispatch_release (group);}//One time-(ibaction) ONEBTN: (ID) sender {}//obstacles-(ibaction) OBSTACLEEBTN: (ID) Sender {//the role of the barrier task: You can guarantee that the concurrency after the obstacle, the task must be completed before the concurrency of the obstacle before the execution can begin//Note: To add an obstacle task, you must use the concurrent queue that you created. //Create a concurrent queuedispatch_queue_t queue = Dispatch_queue_create ("Com.lanou.3g.oa", dispatch_queue_concurrent); //Add a task to the queueDispatch_async (Queue, ^{NSLog (@"a Write file");    }); Dispatch_async (Queue,^{NSLog (@"B Write File");    }); Dispatch_async (Queue,^{NSLog (@"C Write File");    }); Dispatch_async (Queue,^{NSLog (@"d Write File");    }); //Add (Set) barrier taskDispatch_barrier_async (Queue, ^{NSLog (@"Here's the obstacle task .");    }); Dispatch_async (Queue,^{NSLog (@"Read File");    }); //Releasedispatch_release (queue);}//Delay-(ibaction) DELAYBTN: (ID) Sender {dispatch_after (Dispatch_time (Dispatch_time_now) (int64_t) (5* nsec_per_sec)), Dispatch_get_main_queue (), ^{NSLog (@"5 seconds, five seconds"); }); //Dispatch_get_main_queue is executed in the main thread, and if you want to execute it in a child thread, you can change it to a child thread .    }//repeated execution-(ibaction) DIRECEIVEMEMORYBTN: (ID) Sender {//dispatch_apply (dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^ (size_t index) {//NSLog (@ "Number of iterations:%ld, Current thread%@", Index,[nsthread CurrentThread]);//    });dispatch_apply (Ten, Dispatch_get_main_queue (), ^(size_t index) {NSLog (@"number of repeated executions:%LD, current thread%@", Index,[nsthread CurrentThread]); });}
@end

Single case

#import"Singleton.h"@implementation Singleton//+ (ID) mainsingleton{//static Singleton *single = nil;//@synchronized (self) {//if (!single) {//Single = [[Singleton alloc] init];//        }//        //    }//return single;//}+(ID) mainsingleton{StaticSingleton *single =Nil;Staticdispatch_once_t oncetoken;dispatch_once (&oncetoken, ^{    //write code that executes only onceSingle =[[Singleton alloc] init];});returnSingle ;}

Serial, parallel, group, once, obstacle, delay, repeat (GCD) in iOS

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.