nsthread:
– Pros: Nsthread than the other two lightweight, simple to use – Cons: You need to manage your thread's lifecycle, thread synchronization, locking, sleep, and wake up. Thread synchronization has a certain overhead in locking datansoperation:
– No need to worry about thread management, data synchronization, and focus on what you need to do
–nsoperation is an object-oriented
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;
• If you want to update the UI interface, you can call the Performselectoronmainthread method in a background thread hint: Although tasks invoked with the Performselectorinbackground method can update the UI interface, However, in the actual development, it involves updating the UI interface, or using the Performselectoronmainthread method to avoid unnecessary trouble the way to open background tasks -(void) Performselectorinbackground: (SEL) Aselector withobject: (ID) arg notifies the main thread of the method – (void) in the background threads to perform the task Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait; get thread info [ Nsthread currentthread]; thread hibernation [nsthread sleepfortimeinterval:1.0f]; + (void) Detachnewthreadselector: (SEL) selector totarget: (ID) target withobject: (id) argument; -(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) The argument; detachnewthreadselector method will start the threading method directly · Initwithtarget need to call the Start method to be able to start the threading method • Parameter Description: –selector: Thread execution method, only one parameter, cannot have return value –target:selector message sent Object – Argument: The only parameter transmitted to target, or it can be the two subclasses of nil nsoperation 1.NSInvocationOperation 2.nsblockoperation Working principle: 1. Encapsulating the action to be performed with the Nsoperation 2. Place the created Nsoperation object in Nsoperationqueue 3. Start operationQueue starts new thread execution in queue operations • Note: 1. When using multi-threading, it is often necessary to control the number of concurrent threads, because threads consume system resources and run too many threads, and the system slows by 2. The following methods can be used to control the number of concurrent threads: -(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT;
iOS multithreading (Nsthread,nsoperation,grand Central Dispatch)