Some concepts in multi-threading
Task: The code snippet method thread is the execution of these tasks
The Nsthread class creates thread execution threads
[Nsthread ismainthread]//to determine if the main thread
#import "AppDelegate.h"@implementationappdelegate-(void) dealloc{[_window release]; [Super Dealloc];}-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchoptions{Self.window=[[[UIWindow alloc] initwithframe:[[uiscreen mainscreen] bounds]] autorelease]; //Override point for customization after application launch.Self.window.backgroundColor =[Uicolor Whitecolor]; //Implementing Multithreading//Task: The code snippet method thread is the execution of these tasks//The Nsthread class creates thread execution threadsNSLog (@"-------------%d", [Nsthread Ismainthread]);//determine if the main thread//the way to implement multithreading one//using the Performselectorxxx method in the root class NSObject//UI refresh must be done in the main thread thread the UI refreshes with the child thread will not flush abnormally or delay the refresh.//[Self performselectorinbackground: @selector (aa) Withobject:nil]; //How to implement multithreading two//using the Nsthread Class (multithreaded Class)//Create a thread and let the thread execute the AA method of Self//threads are divided into two separate threads and non-detached threads//a thread that is automatically destroyed after playing a task//non-detached threads: threads that are not destroyed after a task is executed and can perform other tasks later//Each thread comes with a Nsrunloop object, and Runloop is turned off by default. Once the Runloop is turned on, your thread becomes non-detached. Runloop help you poll for any new tasks assigned to this thread.//[Nsthread detachnewthreadselector: @selector (aa) totarget:self Withobject:nil]; //To create a thread, the thread needs to perform a task that is the self of the AA method//nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (AA) Object:nil];//[Thread start];//starting the execution thread solves the problem of deferred execution when it wants to execute//the third way to implement multithreading NsoperationqueueNsinvocationoperation *OP1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (AA)Object: nil];//[OP1 start];nsblockoperation*OP2 = [Nsblockoperation blockoperationwithblock:^{NSLog (@"This is a block operation"); }]; //thread synchronization is a dependency between serial threads when synchronization is used to synchronize Nsoperationqueue the maximum number of maxconcurrentoperationcount concurrency is 1Nsoperationqueue *queue =[[Nsoperationqueue alloc] init];//NSLog (@ "~~~~~~~~~~~~~~~~~~~~~~~~%d", queue.maxconcurrentoperationcount);[Queue Setmaxconcurrentoperationcount:1];//set the maximum number of concurrent 1[Queue ADDOPERATION:OP1];//just put the operation into the queue, do not need to call the Start,queue will be based on the OP queuing situation, to help you call start. Smart Help scheduling threads for op[Queue addoperation:op2]; [Self.window makekeyandvisible]; returnYES;}- (void) aa{//If you perform a task in a child thread, we need to add an auto-release pool to the task.//because. threads are independent of each other, but resources are shared, shared heap space, and if a thread opens up space and does not release other threads, it cannot use the space that has been opened .//in particular, when a lot of convenience constructors are used in sub-threads, it can cause many heap spaces to not reclaim memory//thread shared heap, stack does not share each child thread by default to stack size is 512k main thread stack size things 1M stack is mainly put local variables, local variables out of the method will be destroyed, so, although only 512k or 1M but already enough, the size of the stack space must be 4k integer times //@autoreleasepool { for(inti =0; i<Ten; i++) {NSLog (@"%d", i); } NSLog (@"#################%d", [Nsthread ismainthread]); [Self performselectoronmainthread: @selector (BB) withobject:nil Waituntildone:yes]; NSLog (@"the child thread is finished."); }}- (void) bb{@autoreleasepool {NSLog (@"?????????????????????? %d", [Nsthread ismainthread]); for(inti =0; i< -; i++) {NSLog (@"-%d", i); } NSLog (@"Refresh your UI"); }}