This article copies, the reference from the article: iOS multithreading programming nsthread use, mainly in order to strengthen the individual to the knowledge understanding and the memory, does not do his use. Thanks for the hard work of the original author here!
1. Introduction
1.1 iOS Multi-threaded programming technology classification
(1) Nsthread
(2) Cocoa nsoperation
(3) GCD (Grand Central Dispatch)
These three ways from top to bottom, the level of abstraction gradually increased, the use of more and more simple.
1.2 Three ways of pros and cons
|
Advantages |
Disadvantages |
| Nsthread |
Light weight |
You need to manage the thread's life cycle, thread synchronization. When thread synchronization is locked, there is a certain amount of system overhead. |
| Nsoperation |
No need to care about thread management, data synchronization, you can focus on the actions you need to perform |
|
| GCD |
iOS4.0, to replace the nsthread,nsoperation and other technologies, very efficient, powerful |
|
2. Use of Nsthread
2.1 How to create
(1) Instance method creation
-(ID) Initwithtarget: (IDObject:(ID) argument
Example: Nsthread* myThread = [[Nsthread alloc] initwithtarget:self selector: @selector (dosomething:) Object: nil]; [MyThread start];
(2) Nsthread class method creation
+ (void) Detachnewthreadselector: (SEL) aselector totarget: (ID) atarget withobject: (ID anargument example: [Nsthread detachnewthreadselector: @selector (dosomething:) totarget:self Withobject:nil];
(3) NSObject non-display method creation
+ (void) Performselectorinbackground: (SEL) aselector withobject: (ID) anargument example [OBJ Performselectorinbackground: @selector (dosomething:) Withobject:nil];
Selector: Thread execution method, this selector can only have one parameter, and cannot have the return value;
Target:selector object sent by message
Argument: The only parameter transmitted to selector, or nil
The first way is to create the thread directly and start running the thread, and the second method is to create the thread object before running the thread, and then you can set the thread's priority and so on thread information before running the thread operation.
IOS Multi-threaded learning Notes