About -- GCD, -- GCD
GCD is short for Grand Central Dispatch, which can be translated as "awesome Central scheduler". Pure C language provides powerful functions. Two core conceptual tasks in GCD: What operations are performed. Queue: used to store tasks. (In vain, tasks can be executed only when they are placed in the queue .) GCD only uses two steps to customize the task: add the task to the queue for what you want to do: GCD will automatically remove the tasks in the queue and put them in the corresponding thread for execution, task retrieval follows the FIFO principle of the queue: first-in-first-out, and then-out. How does GCD execute the task? There are two functions in GCD for executing the task. 1. Execute the task in synchronous mode.
1 dispatch_sync (dispatch_queue_t queue, dispatch_block_t block); 2 // queue: queue 3 // block: Task
2. execute tasks asynchronously.
1 dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
Differences between synchronous and asynchronous
Synchronization: run in the current thread (the thread is not enabled)
Asynchronous: Execute in another thread (the thread will be enabled)
Queue type
GCD queues can be divided into two types:
1. Concurrent queue
Concurrent queue, which allows concurrent (concurrent) Execution of multiple tasks (automatically enabling multiple threads to execute tasks simultaneously)
The concurrency function is valid only when the asynchronous (dispatch_async) function is used.
By default, GCD provides global concurrent queues for the entire application. You do not need to manually create a global concurrent queue using the dispatch_get_global_queue function.
1 dispatch_queue_t priority (2 bytes priority, // queue priority 3 unsigned long flags); // this parameter is useless for the moment. Use 0 to 4 dispatch_queue_t queue = dispatch_get_global_queue (priority, 0 ); // obtain the global concurrency queue
Priority of global concurrent queues
1 # define DISPATCH_QUEUE_PRIORITY_HIGH 2 // high 2 # define DISPATCH_QUEUE_PRIORITY_DEFAULT 0 // default (medium) 3 # define partition (-2) // low 4 # define partition INT16_MIN/background
2. Serial queue
Let the task be executed one by one (after a task is executed, execute the next task)
There are two methods to obtain serial numbers in GCD.
1. Use the dispatch_queue_create function to create a serial queue
1 dispatch_queue_t2 dispatch_queue_create (const char * label, // queue name 3 dispatch_queue_attr_t attr); // queue attribute, generally use NULL to 4 dispatch_queue_t queue = dispatch_queue_create ("cn. itcast. queue ", NULL); // create 5 dispatch_release (queue); // non-ARC queue needs to be released manually
Ii. Use the main queue column (queue associated with the main thread)
The main queue is a special serial queue that comes with GCD.
All tasks in the main queue are executed in the main thread.
Use dispatch_get_main_queue () to obtain the main queue Column
1 dispatch_queue_t queue = dispatch_get_main_queue();
------ Summary ------
Synchronization and Asynchronization determine whether to enable new threads
Synchronization: The task is executed in the current thread and cannot start a new thread.
Asynchronous: execute tasks in new threads. Able to start new threads.
Concurrency and serial determine the task execution mode.
Concurrency: multiple tasks (at the same time) are executed.
Serial: After a task is executed, the next task is executed.
** Inter-thread communication **
The subthread returns to the main thread.
1 // create a global concurrent queue 2 dispatch_queue_t queue = dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 3 // Add a task to the queue and execute 4 dispatch_async (queue, ^ {5 NSLog (@ "---- 111 ---- % @", [NSThread currentThread]); 6 // download Picture 7 NSURL * url = [NSURL URLWithString: @ "http://5.26923.com/download/pic/000/328/ba80a24af0d5aba07e1461eca71f9502.jpg"]; 8 NSData * data = [NSData dataWithContentsOfURL: url]; 9 UIImage * image = [UIImage imageWithData: data]; 10 // The main back thread displays the image 11 dispatch_async (dispatch_get_main_queue (), ^ {12 NSLog (@ "------ 22222 ---- % @", [NSThread currentThread]); 13 self. image1View. image = image; 14}); 15 });
** Delayed execution **
1. Call the NSObject Method
1 [self execution mselector: @ selector (run) withObject: nil afterDelay: 2.0]; 2 // 2 seconds later, call self's run Method
Ii. Use the GCD Function
1 dispatch_after (dispatch_time (DISPATCH_TIME_NOW, (int64_t) (2.0 * NSEC_PER_SEC), dispatch_get_main_queue (), ^ {2/2 seconds later, asynchronously execute the code here... 3 4 });
** One-time code **
The dispatch_once function ensures that a piece of code is executed only once during the program running.
1 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{2 NSLog(@"------111------%@",[NSThread currentThread]);3 });
** Queue group **
A queue group is suitable for asynchronous execution of two time-consuming operations. After two asynchronous operations are completed, the operation is executed in the return to the main thread.
1 dispatch_group_t group = dispatch_group_create (); 2 dispatch_group_async (group, dispatch_get_global_queue (queue, 0), ^ {3 // execute 1 time-consuming Asynchronous Operation 4 }); 5 dispatch_group_async (group, dispatch_get_global_queue (queue, 0), ^ {6 // execute 1 time-consuming Asynchronous Operation 7}); 8 dispatch_group_quey (group, dispatch_get_main_queue (), ^ {9 // after all the preceding asynchronous operations are completed, return to the main thread... 10 });
The above is my personal notes during study. If you find something wrong or have a better way, please let me know. So that I can correct it. Thank you very much.