1. Deferred execution
/*********************** Delay the first method of **************************/ /*The first method calls the NSObject method [self performselector: @selector (Run) Withobject:nil afterdelay:2.0]; @1. The method is called on that thread, and run is on which thread (the current thread) is executing, usually the main thread. @2. If the method is executed in an asynchronous function, the method is not called*///First Method: Delay of 3 seconds call the Run function/*NSLog (@ "Print thread----%@", [Nsthread CurrentThread]); [Self performselector: @selector (Run) Withobject:nil afterdelay:2.0]; *//********************** Delay the second method of **********************/ /**************** Home Row ***************/ //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]); }); /**************** Concurrent Queue ***************/ //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. Disposable code
/************************** Method 1*************************/ //Cons: This is an object method, and if you create a new controller again, the print code executes because each newly created controller has its own Boolean type, and the newly created default is no, so it is not guaranteed that the row code will be printed only once throughout the program. if(_log==NO) {NSLog (@"Method 11 The code of the minor"); _log=YES; }/*************************** Method 2**************************/ //Use the Dispatch_once function to ensure that a piece of code is executed only 1 times during program Operation//The entire program runs, only once. Staticdispatch_once_t once; Dispatch_once (&once,^{NSLog (@"Method 21 The code of the minor");//execute code only 1 times (this is thread-safe by default)});
3. Queue Groups
/************************* Queue Group **************************//*tip: Using a queue group allows the download of pictures 1 and 2 to be performed simultaneously, and when the two download tasks are completed, they are returned to the main thread for display. 2. Using queue groups To resolve steps: 2.1 Create a group 2.2 open a task download Picture 1 2.3 Open a task download picture 2 2.4 Execute download picture simultaneously 1\ download Picture 2 operation 2.5 All tasks in group are executed, then back to the main line Process to perform other operations*/NSLog (@"Queue table Start download"); dispatch_group_t Group=dispatch_group_create (); /******************************************/ //perform 1 time-consuming asynchronous operations__block UIImage *image1=Nil; Dispatch_group_async (Group,global_quque,^{NSLog (@"image 1 Start download"); Image1=[self Imagewithurl:@"http://a583.phobos.apple.com/us/r30/Purple4/v4/02/23/ec/0223ec03-ed8e-1c92-26c0-7dec34de6667/ Mzl.ksnlsaoo.175x175-75.png"]; NSLog (@"picture 1 Download Complete--%@", [Nsthread CurrentThread]); }); /*******************************************/ //perform 1 time-consuming asynchronous operations__block UIImage *image2=Nil; Dispatch_group_async (Group, Global_quque,^{NSLog (@"image 2 Start download"); Image2=[self Imagewithurl:@"http://img3.imgtn.bdimg.com/it/u=1790102556,3036052735&fm=21&gp=0.jpg"]; NSLog (@"Picture 2 Download Complete--%@", [Nsthread CurrentThread]); }); /********************************************/ //when the previous asynchronous operation is complete, return to the main thread ...Dispatch_group_notify (Group, main_queue, ^{self.imageView1.image=Image1; Self.imageView2.image=Image2; Uigraphicsbeginimagecontextwithoptions (Cgsizemake ( $, -), NO,0); [Image1 Drawinrect:cgrectmake (0,0, -, -)]; [Image2 Drawinrect:cgrectmake ( -,0, -, -)]; Self.imageView3.image=Uigraphicsgetimagefromcurrentimagecontext (); Uigraphicsendimagecontext (); NSLog (@"picture Merge Complete%@", [Nsthread CurrentThread]); });
/* ************************************************************ */-(uiimage*) Imagewithurl: (nsstring*) urlstr{*url=[Nsurl urlwithstring:urlstr] ; *data=[NSData Datawithcontentsofurl:url]; *image=[UIImage imagewithdata:data]; return image;}
Common usage of thread 8--GCD