1. What is GCD
A. Full name is the Grand Center Dispatch
B. Pure C language, which provides a very large number of powerful functions
2. Advantages of GCD
A. gcd is Apple's solution for multi-core parallel computing
B. GCD will automatically take advantage of more CPU cores (such as dual-core, quad-core)
C. GCD automatically manages the life cycle of threads (create threads, schedule tasks, destroy threads)
D. Programmers need not write any thread-managed code just to tell gcd what tasks they want to perform
3. GCD two core concepts- tasks and Queues
Task : Action to be performed (method) using block encapsulation, block is a well-prepared block of code that executes when needed
Functions that perform tasks
Synchronous Execution : The current instruction is not completed and the next instruction is not executed Dispatch_sync (queue, Task)
Asynchronous Execution : The current instruction is not complete, you can execute the next instruction Dispatch_async (queue, Task)
Queue : A collection used to hold tasks
serial Queue : one-by-one scheduling task dispatch_queue_t q = dispatch_queue_create ("JS", NULL);
concurrent Queues: multiple tasks can be scheduled at the same time dispatch_queue_t q = dispatch_queue_create ("JS", Dispatch_queue_con current);
Home column : Global serial queue, which dispatches tasks serially by the main thread, and only one dispatch_queue_t q = dispatch_get_main_queue ();
Global Queue : Concurrent queue with no name dispatch_queue_t q = dispatch_get_global_queue (0, 0);
4. GCD Use steps
A. Customizing tasks: Identifying what you want to do
B. Adding a task to a queue and performing tasks in the specified synchronous or asynchronous manner
I. GCD will automatically take the tasks in the queue and put them in the corresponding thread.
Ii. task Removal follows the FIFO principle of the queue, first-in, FIFO
5. code example:
A. Synchronous execution
//1 Queues This queue is globaldispatch_queue_t q = Dispatch_get_global_queue (0,0);//2 Questsvoid(^task) () = ^{[Nsthread sleepfortimeinterval:1]; NSLog (@"%@", [Nsthread CurrentThread]); };//3 Add to queue and specify the execution method//synchronous execution, this method does not open the threadDispatch_sync (q, Task); NSLog (@" Over");
B. Asynchronous execution
//executes asynchronously, opening a new thread//This method simply adds the task to the queue and then gcd the managed thread pool with idle threads that take the task from the queue to execute//1 Queues This queue is globaldispatch_queue_t q = Dispatch_get_global_queue (0,0);//2 Questsvoid(^task) () = ^{[Nsthread sleepfortimeinterval:1]; NSLog (@"%@", [Nsthread CurrentThread]); };d Ispatch_async (q, Task); NSLog (@" Over");
C. Inter-thread communication
//This method simply adds the task to the queue and then gcd the managed thread pool with idle threads that take the task from the queue to executeDispatch_async (Dispatch_get_global_queue (0,0), ^{//Child ThreadsNSLog (@"%@", [Nsthread CurrentThread]);//Download ImageNsurl *url = [Nsurl urlwithstring:@"http://127.0.0.1/pic.jpg"]; NSData*data =[NSData Datawithcontentsofurl:url]; UIImage*image =[UIImage Imagewithdata:data];//inter-thread communication, assigning a value to a control, should be in the UI thread//do not directly manipulate threads in GCD, just put the code in the main queue and OK//the tasks in the main queue are called by the main threadDispatch_async (Dispatch_get_main_queue (), ^{self.imageView.image=image; [Self.imageview SizeToFit]; Self.scrollView.contentSize=image.size; Self.scrollView.backgroundColor= [Uicolor Graycolor];//Main ThreadNSLog (@"%@", [Nsthread CurrentThread]); });});
IOS-Multithreading-GCD-First knowledge