iOS multithreading (Nsthread,nsoperation,grand Central Dispatch)

Source: Internet
Author: User

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)

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.