Nsthread
-(void) commonthread{ /** using Nsthread is not a convenient place to create a new method every time you use a thread. But it's a good way to remember, easy to use, just a little bit weak in function. */ //nsthread Two types of use //1. Nsthread *thread1 = [[Nsthread alloc]initwithtarget:self selector: @selector (READNUM1) Object:nil]; [Thread1 start]; 2. [Nsthread detachnewthreadselector: @selector (loadthreadargumentwithstr:) totarget:self withobject:@ "argument"]; Go back to the main thread to do something (the UI can only be refreshed in the main thread, of course, it's OK to do something else) [self performselectoronmainthread: @selector (readNum2) Withobject: Nil Waituntildone:no]; NSObject-a method of an object can run directly in the background [self performselectorinbackground: @selector (loadthreadargumentwithstr:) withobject:@ "Nsobjectthread"];}
Nsoperation&nsoperationqueue
Nsoperationqueue will build a thread manager, and each join to the thread operation will be executed in an orderly manner. -(void) loadoperation{/** [XXXXX New] ~= [[XXXX Alloc]init] New is implicitly called Init, if using alloc/init we can customize Initxxx */N Soperationqueue *queue = [Nsoperationqueue new]; Sets the maximum number of concurrent threads [queue setmaxconcurrentoperationcount:2]; Nsinvocationoperation *operation =[[nsinvocationoperation alloc] initwithtarget:self selector: @selector ( threadreadnums) Object:nil]; Nsinvocationoperation *operation2 =[[nsinvocationoperation alloc] initwithtarget:self selector: @selector ( THREADREADNUMS2) Object:nil]; Nsinvocationoperation *operation3 =[[nsinvocationoperation alloc] initwithtarget:self selector: @selector ( threadreadnums) Object:nil]; [Queue addoperation:operation]; [Queue Addoperation:operation2]; [Queue Addoperation:operation3]; Nsblockoperation *blockoperation = [nsblockoperation blockoperationwithblock:^{for (int i=0; i<10; i + +) {NSLog (@ "Nsblockoperation-->%zi", i); } }]; [Queue addoperation:blockoperation];}
adding dependencies
-(void) addpriorityoperation{nsoperationqueue *queue = [Nsoperationqueue new]; Nsblockoperation *blockoperation1 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 1"); }]; Nsblockoperation *blockoperation2 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 2"); }]; Nsblockoperation *blockoperation3 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 3"); }]; Nsblockoperation *blockoperation4 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 4"); }]; Nsblockoperation *blockoperation5 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 5"); }]; Nsblockoperation *blockoperation6 = [nsblockoperation blockoperationwithblock:^{NSLog (@ "Blockoperation 6"); }]; [BlockOperation6 Setqueuepriority:nsoperationqueuepriorityhigh]; [BlockOperation6 adddependency:blockoperation1];//dependency is better than priority setting [BlockOperation1 Adddependency:blockoperation2]; [BlockOperation2 Adddependency:blockoperation3]; [BlockOperation3 Adddependency:blockoperation4]; [BlockOperation4 Adddependency:blockoperation5]; [Queue Setmaxconcurrentoperationcount:6]; [Queue Addoperation:blockoperation1]; [Queue Addoperation:blockoperation2]; [Queue Addoperation:blockoperation3]; [Queue Addoperation:blockoperation4]; [Queue addoperation:blockoperation5]; [Queue addoperation:blockoperation6];}
Use of GCD
1-(void) Dispatchmethods2 {3 //1. Background execution4Dispatch_async (Dispatch_get_global_queue (0,0), ^{5 //dosomething6 });7 //2. Main thread Execution8Dispatch_async (Dispatch_get_main_queue (), ^{9 //dosomethingTen }); One //3. One-time implementation A Staticdispatch_once_t Oncetoken; -Dispatch_once (&oncetoken, ^{ - //The code will be executed once the }); - //4. Delay Execution -dispatch_time_t time = Dispatch_time (Dispatch_time_now,2.0*nsec_per_sec); -Dispatch_after (Time, Dispatch_get_main_queue (), ^ (void){ + //2 seconds to execute . - }); +Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{ A //2 seconds to execute . at }); - //5. Customizing the way to use the Idspatch_queue_create method -dispatch_queue_t Urls_queue = Dispatch_queue_create ("URL", NULL); -Dispatch_async (Urls_queue, ^{ - //Execute Code - }); in //6. Execute multiple threads in parallel in the background and summarize execution results after execution. - //implemented with Dispatch_group,dispatch_async and dicpatch_group_notify todispatch_group_t Group =dispatch_group_create (); +Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{ - //Thread Task One the }); *Dispatch_group_async (Group, Dispatch_get_global_queue (0,0), ^{ $ //Thread Task twoPanax Notoginseng }); -Dispatch_group_notify (Group, Dispatch_get_global_queue (0,0), ^{ the //Summary Results ... This method is executed when all the thread tasks in the group are completed + }); A}
Multi-threaded learning and consolidation