Multithreading
Personal understanding generally a program has a main thread, mainly control the click, UI interface, if a time-consuming operation, the user experience is quite rubbish, so we will talk about executing time-consuming operation to a new thread, but with thread to beware of thread security issues, such as (shared resources) cause data confusion such as ticket task , Bank savings, etc. (solution using @synchronized (lock object)) Apple offers a gcd called the Central scheduler that works by putting the time-consuming operations we are going to perform in a queue, GCD provides a way to get the task out of the queue to open thread execution, So the thread we don't have to write code GCD there are two functions that perform tasks one is the synchronous function Dispatch_sync, the other is the asynchronous function DISPATCH_ASYSC, synchronous: Executing in the current thread, asynchronously: executing in another thread The GCD queue can also be divided into two:11 is a concurrent queue: Multiple tasks can be concurrent (simultaneous)Execute (will automatically open multiple threads)
The GCD default already provides a global concurrency queue for the entire app to use, without the need to manually create
Using the Dispatch_get_global_queue function to obtain a global concurrency queue
Description: Priority for global concurrent queues
#define DISPATCH_QUEUE_PRIORITY_HIGH 2//High
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0//default (Medium)
#define Dispatch_queue_priority_low (-2)//Low
#define Dispatch_queue_priority_background int16_min//Backstage
21 are serial queues: that is, the task executes sequentially (in one thread)Serial queues have two functions for creating queues: 1>dispatch_queue_t dispatch_queue_create (creating serial queues) 2>dispatch_queue_t Dispatch_get_main_ Queue (home row, queues associated with mainline threads)
code example:
Adding tasks to concurrent queues with asynchronous functions
#import "LLBViewController.h"@interfaceLlbviewcontroller ()@end@implementationLlbviewcontroller- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //1, get global concurrency queuedispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //2, add a task to the queue, you can perform the task//Async Functions: Ability to open new threadsDispatch_async (Queue, ^{NSLog (@"Download Movie 1-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue,^{NSLog (@"Download Movie 2-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue,^{NSLog (@"Download Movie 3-----%@", [Nsthread CurrentThread]); }); //Print main threadNSLog (@"Main thread-----%@", [Nsthread CurrentThread]);}
Note: Open up to 3 threads at a time
2> asynchronous serial queue with open new thread
- (void) viewdidload{[Super Viewdidload]; //additional setup after loading the view, typically from a nib. //Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("NIUBIGCD", NULL); //The first parameter is the name of the serial queue, which is the C language string//The second is the property of the queue, which generally says that the serial queue does not have to assign any attributes, null//Add a task to the queue to executeDispatch_async (Queue, ^{NSLog (@"Download Image 1-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue,^{NSLog (@"Download Image 2-----%@", [Nsthread CurrentThread]); }); //non-ARC needs to release the queue general new,alloc,retain,create needs to free up memory because the system allocates memory when creating objectsNSLog (@"Main thread------%@", [Nsthread CurrentThread]);}
the-Ten- + -: -:11.354gcd[2347:907] Main thread------<nsthread:0x7541aa0>{name = (NULL), num =1} the-Ten- + -: -:11.354gcd[2347:1103] Download Picture 1-----<nsthread:0x757d2e0>{name = (NULL), num =3} the-Ten- + -: -:11.356gcd[2347:1103] Download Picture 2-----<nsthread:0x757d2e0>{name = (NULL), num =3}
Note: The new thread is turned on, but the new thread has only one;
3> synchronization function Parallel queue Add task
- (void) viewdidload{[Super Viewdidload]; NSLog (@"main thread--------%@", [Nsthread CurrentThread]); //synchronizing functions adding tasks in concurrent queuesdispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default,0); //add a task to a queueDispatch_sync (Queue, ^{NSLog (@"Download Movie 1-----%@", [Nsthread CurrentThread]); }); //add a task to a queueDispatch_sync (Queue, ^{NSLog (@"Download Movie 2-----%@", [Nsthread CurrentThread]); });}
2014-10-21 16:34:55.725 gcd[2374:907] Main thread--------<nsthread:0x762a0a0>{name = (null), num = 1}2014-10-21 16:34:55.726 gcd[2374:907] Download Movie 1-----<nsthread:0x762a0a0>{name = (null), num = 1}2014-10-21 16:34:55.726 GCD[2374 : 907] Download Movie 2-----<nsthread:0x762a0a0>{name = (null), num = 1}
Note: The sync function does not have the ability to open a new thread, taking advantage of the current thread
4> adding tasks to the serial queue of synchronous functions
-(void) viewdidload{ [Super Viewdidload]; NSLog (@ "main thread--------%@", [Nsthread CurrentThread]); Sync function Serial Queue Add task dispatch_queue_t queue = dispatch_queue_create ("NIUBIGDC", NULL); Add a task to the queue Dispatch_sync (queue, ^{ NSLog (@ "Download Movie 1-----%@", [Nsthread CurrentThread]) ; Add a task to the queue Dispatch_sync (queue, ^{ NSLog (@ "Download Movie 2-----%@", [Nsthread CurrentThread]); }
2014-10-21 16:38:36.116 gcd[2391:907] Main thread--------<nsthread:0x763e4a0>{name = (null), num = 1}2014-10-21 16:38:36.117 gcd[2391:907] Download Movie 1-----<nsthread:0x763e4a0>{name = (null), num = 1}2014-10-21 16:38:36.118 GCD[2391 : 907] Download Movie 2-----<nsthread:0x763e4a0>{name = (null), num = 1}
Note: The new thread is not opened, or the current main threads
Multithreaded gcd-Center Scheduler