1, Introduction: 1.1 iOS has three kinds of multi-threaded programming technology, respectively:
1., Nsthread
2. Cocoa nsoperation (use of nsoperation and Nsoperationqueue for iOS multithreaded programming)
3. GCD Full Name: Grand Central Dispatch (Introduction and use of the Grand Central Dispatch (GCD) for iOS multithreaded programming
These three kinds of programming from top to bottom level of abstraction from low to high, the higher the abstraction of the more simple to use, is also the most recommended for Apple.
In this article we mainly introduce and use Nsthread, the following will continue to 2, 3 of the explanation and use.
1.2 Three ways of having disadvantages are introduced:
Nsthread:
Pros: Nsthread than the other two lightweight
Cons: You need to manage your thread's life cycle, thread synchronization. Thread synchronization has a certain overhead in locking data
Nsthread is created in two ways:
You can see from the following code that Nsthread is concurrent synchronous execution
- (void) viewdidload {[Super viewdidload]; [Nsthread detachnewthreadselector: @selector (myThread) totarget:self Withobject:nil]; NSLog (@"234");}-(void) mythread{Sleep (5); NSLog (@"123");}2.- (void) viewdidload {[Super viewdidload]; Nsthread* Mathread = [[Nsthread alloc] initwithtarget:self selector: @selector (MyThread)Object: nil]; [Mathread start]; NSLog (@"234");}-(void) mythread{Sleep (5); NSLog (@"123");}
The first way is to create the thread directly and start running the thread, the second way is to create the thread object before running the thread operation, you can set the thread's priority and so on thread information before running the thread operation
2.4.2 Inter-thread communication
How to notify the main thread update interface after downloading the picture?
[self performselectoronmainthread:@selector(updateUI:) withobject: Image Waituntildone :YES];
Performselectoronmainthread is the NSObject method, in addition to updating the main thread's data, you can also update other threads such as:
By: PerformSelector:onThread:withObject:waitUntilDone:
iOS Development Threading Summary--nsthread