Introduction: The Application of threading in iphone is not unrestrained, the official data show that the main thread of the iphone OS stack size is 1M, the second thread starts with 512KB. And the value cannot be changed by the compiler switch or the thread API function.
There are 3 main ways to implement iOS Multithreading: Nsthread, Nsoperation, GCD.
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 mainly introduces the use of Nsthread
First, create
There are 3 main ways in which thread creation works:
-(ID) init; // designated initializer-(ID) Initwithtarget: (IDObject:(ID) Argument Target parameter: Selector The object sent by the message
Of course, there is one more special, that is, using the so-called convenient method, which can generate a thread directly and start it without being responsible for the cleanup of the thread. The interface for this method is :
+ (void) Detachnewthreadselector: (SEL) aselector totarget: (ID) atarget withobject: (ID) Anargument
There is also an implicit way to create threads:
[Self Performselectorinbackground: @selector (Run) Withobject:nil];
After the first two methods are created, you need to start them manually, starting with the following methods:
-(void) start;
Second, get the current thread and main threads
Nsthread *current =*main = [Nsthread mainthread];
Iii. Suspension of
// pause 2s [Nsthread sleepfortimeinterval:2]; // or NSDate *date = [nsdate datewithtimeinterval:2 sincedate:[nsdate Date]]; [Nsthread sleepuntildate:date];
Iv. Communication between threads
// perform actions on the specified thread [Self performselector: @selector (run) onthread:thread Withobject:nil Waituntildone:yes]; // performing actions on the main thread [Self performselectoronmainthread: @selector (Run) Withobject:nil Waituntildone:yes]; // performing actions on the current thread
V. 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
iOS Multithreading series (i)------nsthread