Multi-Threading implementations in iOS:
| Technology |
Language |
Thread life cycle |
Frequency of Use |
| Pthread |
C |
Programmers manage themselves |
Almost no |
| Nsthread |
Oc |
Programmers manage themselves |
Occasional use |
| GCD |
C |
Automatic Management |
Frequently used |
| Nsoperation |
Oc |
Automatic Management |
Frequently used |
The state of the thread
how Nsthread is created :
Create thread mode one nsthread *threadone = [[Nsthread alloc] initwithtarget:self selector: @selector (testaction) Object:nil]; //Give the thread a name Threadone.name = @ "Threadone"; Start the thread, execute the Testaction method on the newly opened thread [threadone start]; Thread mode Two is created and will start automatically [Nsthread detachnewthreadselector: @selector (testaction) totarget:self Withobject:nil]; Create thread mode three, implicit creation, auto-start [self performselectorinbackground: @selector (testaction) Withobject:nil];
method to invoke
-(void) testaction{ for (int i = 0; i < 3; i++) { NSLog (@ "i =%d, current thread =%@", I,[nsthread CurrentThread]); }}
Result: You can see that there are 3 threads executing concurrently
Properties of the thread:
Create a thread nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (testaction) Object:nil]; Thread name Thread.Name = @ "WL"; Thread priority, general not set, default 0.5, numeric range 0-1, the higher the value the higher the priority thread.threadpriority = 0.5;
Common methods, which are class methods that operate relative to the thread in which the code resides
Get the main thread [Nsthread mainthread]; Determines whether the main thread, returns a bool value bool Ismainthread = [Nsthread ismainthread]; Returns a bool value bool ismultithreaded = [Nsthread ismultithreaded] to determine whether it is multi-threaded; Remove a thread from a pool of scheduled threads 2s (blocking threads) [Nsthread sleepfortimeinterval:2]; Remove threads from the pool of scheduled threads until a point in time (blocking threads) [Nsthread sleepuntildate:[nsdate Datewithtimeintervalsincenow:2]]; Stop thread, thread dead, this thread no longer exists [Nsthread exit];
Multithreading (i) Nsthread