Each IOS application has a main thread that is designed to update the UI interface and handle the user's touch events, so it is not possible to put other too-time-consuming operations on the main thread, or it will cause the main thread to clog (the phenomenon of card-machine), resulting in a very bad user experience. The general solution is to put those time-consuming operations into another thread to execute, multithreading is the best way to prevent the main thread from clogging and increase the operational efficiency.
iOS supports multiple levels of multithreaded programming, the higher the level of abstraction, the more convenient to use, but also Apple's most recommended method. The following lists the multithreaded programming methods supported by iOS, from low to high, based on the level of abstraction:
1.Thread : Three methods are relatively lightweight, but need to manage the life cycle of the thread, synchronization, lock issues, which can lead to a certain performance cost
2.Cocoa Operations: Based on OC implementations, Nsoperation encapsulates the actions that need to be performed in an object-oriented manner, without concern for thread management, synchronization, and so on. Nsoperation is an abstract base class, and iOS provides two default implementations: Nsinvocationoperation and Nsblockoperation, and of course you can customize nsoperation
3.Grand Central Dispatch(Gcd,ios4 only started): Provides new features, runtime to support multicore parallel programming, with a higher focus: how to increase efficiency on multiple CPUs
This article briefly describes the first multithreaded programming approach, mainly using the Nsthread class, a Nsthread instance that represents a thread
First, the initialization of Nsthread
1. Dynamic Methods
-(ID) Initwithtarget: (IDObject:(ID) argument;
// Initializing Threads Object : nil]; // to set the priority of a thread (0.0-1.0,1.0 highest level) 1 ; // Open Thread [Thread start];
Parameter resolution:
selector : Thread execution method, this selector can only receive one parameter at most
target : Selector object sent by message
argument : The only parameter passed to selector, or nil
2. Static methods
+ (void) Detachnewthreadselector: (SEL) selector Totarget: (ID) target withobject: (ID
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil]; // when the call is complete, a new thread is created and opened immediately
3. Methods for creating threads implicitly
[Self Performselectorinbackground: @selector (Run) Withobject:nil];
Second, get the current thread
Nsthread *current = [Nsthread CurrentThread];
Third, get the main thread
Nsthread *main = [Nsthread mainthread];
Iv. pausing the current thread
// pause 2s [Nsthread sleepfortimeinterval:2]; // or NSDate *date = [nsdate datewithtimeinterval:2 sincedate:[nsdate Date]]; [Nsthread sleepuntildate:date];
V. Communication between threads
1. Perform actions on the specified thread
[Self performselector: @selector (run) onthread:thread Withobject:nil Waituntildone:yes];
2. Perform actions on the main thread
[Self performselectoronmainthread: @selector (Run) Withobject:nil Waituntildone:yes];
3. Performing actions on the current thread
[Self performselector: @selector (Run) Withobject:nil];
Vi. Advantages and Disadvantages
1. Advantages: Nsthread is lighter than the other two multithreaded schemes and controls threading objects more intuitively
2. Cons: You need to manage the thread's life cycle, thread synchronization. Thread synchronization has a certain overhead in locking data
From: http://blog.csdn.net/q199109106q/article/details/8565844
iOS development multithreaded Programming-Nsthread