There are 4 ways to implement multi-threading in iOS
1.pthread: A set of universal multi-threaded api/for unix\linux\windows systems (cross-platform portability/use is difficult) C language almost no
2.NSThread: Using more object-oriented/easy-to-use, direct manipulation of thread object program management (thread life cycle) OC Language Occasional use
3.GCD: Multi-core automatic management, which is designed to replace Nsthread and other multi-threading technologies/fully utilized devices C language is frequently used
4.NSOperation: Based on gcd (bottom gcd)/More simple use function than GCD/Use more object-oriented automatic management OC language often
Print thread flag number, the main thread is 1: [Nsthread CurrentThread]
3 Ways to create threads (new ones are child threads)
1>nsthread OC Developer Control (management) [thread creation and destruction] 3 method created by the thread execution @selelct (method) is seldom used in the work
1>nsthread *thread=[nsthread alloc]initwithtarget:self Selector: @selector (add) Object:nil];
Call the Add method
[Thread start] must be called;
2.[self Performselectorinbackground: @select (add) Withobject:nil]
3.[nsthread Detachnewthreadselector: @selelct (add) totarget:self Withobject:nil]
2>GCD Pure C Threading System automatic management, make full use of multi-core technology often used
3> Operation GCD-based OC implementations are often used
Thread synchronization, multiple lines call the same method (example add) add a lock to the method (lock object, which requires the thread to use the same lock)
After locking, it is when one thread finishes executing another thread
If not locked, the thread randomly (in the cache pool to find the thread) calls
@synchronized (self)
{//Locking
for (int i0;i<100;i++) {
NSLOg (@ "%@", [Nsthread CurrentThread]);
}
}//Unlocking
Communication between threads (interaction, calling a method in a newly created child thread, using the main thread in a method to do something, generating a thread of communication)
1. Create a child thread [self performselectorinbackground: @selector (downloadimage) Withobject:nil]; Call Downloadimage method
2. Use the main thread in the Downloadimage method to download the image [self performselectoronmainthread: @selector (displayphoto:) withobject:img Waituntildone:no];
3. In Displayphoto: Method Display-(void) Displayphoto: (UIImage *) img {self.photoview.image=img;}
There are two methods of operation and queue in GCD, the operation is divided into (synchronous and asynchronous), the queue is divided into (concurrent execution of multiple blocks at a time) and serial [only one block at a time]).
Common combinations are: 1. Asynchronous concurrency
dispatch_queue_t queue=dispatch_get_global_queue (dispatch_queue_priority_default, 0);//Get Global concurrent queue
Asynchronous operation: Dispatch_async (queue, ^{NSLog (@ "%@", [Nsthread CurrentThread]); });
2. Async + Home Row
dispatch_queue_t queue=dispatch_get_main_queue ();//Get the home row
Asynchronous operation: Dispatch_async (queue, ^{NSLog (@ "%@", [Nsthread CurrentThread]); });
3. Asynchronous + Serial
dispatch_queue_t queue=dispatch_queue_create ("com", NULL);//Create a serial queue
Asynchronous operation: Dispatch_async (queue, ^{NSLog (@ "%@", [Nsthread CurrentThread]); });
4. Synchronization + concurrency
dispatch_queue_t queue=dispatch_get_global_queue (dispatch_queue_priority_default, 0);//Get Global concurrent queue
Synchronous operation: Dispatch_sync (queue, ^{NSLog (@ "%@", [Nsthread CurrentThread]); });
GCD communication (Create a global concurrent/serial queue, in a synchronous/asynchronous queue, create the main thread to display)
Nsoperation nsoperationqueue *queue=[[nsoperationqueue alloc] init];
Add dependencies (that is, set Nsblockoperation to create multiple b1,b2,b3)
Nsblockoperation *b1=[nsblockoperation blockoperationwithblock:^{NSLog (@ "1");}];
[B3 ADDDEPENDENCY:B2]; [B2 ADDDEPENDENCY:B1]; Only when B1 executes B2 b1->b2->b3
Add b1,b2,b3 again to nsoperation queue addoperation:b1/b2/b3
Communication as above, gets the main thread action in the newly created threading nsoperation
Small summary of iOS multi-threading