GCD Usage Summary
Watched MJ great God videos with several podcasts in-depth understanding Gcd:part 1/2 in-depth understanding of Gcd:part 2/2 GCD use of experience and skills after a brief discussion of their own summary of the use of attention
Task
The actions to be performed in GCD can be called tasks (downloading pictures, downloading text, etc.)
Queue
In order to help us schedule the task according to FIFO(first-in, in-out), GCD will take the task that we added to the queue and execute it in the thread. Queues are divided into
Serial queue
Serial queues can only have one task at a time, and the latter will not be dispatched to the thread until the previous task has finished executing
Parallel queues
Parallel queues can be executed on multiple tasks at the same time , and after a task does not need to wait for the previous task to finish, it can be dispatched
Submission of tasks
The way the task is submitted determines whether or not to open a new thread , and there are two ways to add a task to the queue in GCD
Synchronous commit
The task is submitted synchronously to the queue, the new thread is not opened, it is executed only on the current thread , and only after the synchronization task has finished executing can you continue to execute the code down and synchronize the commit:
1 dispatch_sync (dispatch_queue_t queue, dispatch_block_t block);
Asynchronous commit
The task is committed asynchronously to the queue, the new thread is opened, but the number of new threads is opened, and we cannot decide, by the system itself, and without waiting for the asynchronous task to execute, to proceed with the execution of the code in the asynchronous way:
1 dispatch_async (dispatch_queue_t queue, dispatch_block_t block);
Different ways of submitting to different queues in GCD
Serial Queue Synchronous Commit
1 dispatch_queue_t queue = dispatch_queue_create ("com.yys.test", Dispatch_ queue_serial); 2 3 dispatch_sync (queue, ^{45// download files, pictures etc Resources 6 7 });
First, based on synchronous commit, no new thread is turned on
Then, depending on the characteristics of the serial queue, only one task can be performed at the same time
Finally, the new thread is not opened, and the task executes sequentially in the current thread in the order in which they were submitted
Serial Queue Asynchronous Commit
1 dispatch_queue_t queue = dispatch_queue_create ( " com.yys.test " , dispatch_queue_serial); 2 { 4 5 // download files, pictures and other resources
6 7 });
Depending on asynchronous commit, a new thread will open
You can perform only one task at a time based on the characteristics of a serial queue
Final Get opens a new thread, and the task executes sequentially in the newly opened thread, in order of submission, but is opened, because the asynchronous commit opens a new thread, but the serial queue requires only one thread to perform all the committed tasks
Parallel Queue Synchronous Commit
1 dispatch_queue_t queue = dispatch_queue_create ("com.yys.test", Dispatch_ queue_concurrent); 2 3 dispatch_sync (queue, ^{45// download files, pictures etc Resources 6 7 });
No new thread is turned on according to synchronous commit
Parallel queues can have multiple tasks scheduled at the same time, but under the condition of synchronous commit, the parallel queue loses the ability to parallelize , and the serial queue differs little
Finally, the new thread is not opened, and the submitted task executes in the current thread in the order in which it was submitted
Parallel queue Asynchronous Commit
1 dispatch_queue_t queue = dispatch_queue_create ("com.yys.test", Dispatch_ queue_concurrent); 2 3 dispatch_async (queue, ^{45// download files, pictures etc Resources 6 7 });
Asynchronous commit to open a new thread
Parallel queues can have multiple tasks scheduled at the same time
Finally, a new thread is turned on, multiple tasks can be executed at the same time, multiple threads will be opened , but we cannot control how many threads are opened. So, it can be used to download multiple images at the same time
GCD in special queue, home column (serial queue)
The primary queue is a serial queue, and its primary function is to update the UI controls, and all UI controls must be refreshed in the main thread
Home Row Synchronous commit (Note in particular)
1 dispatch_queue_t queue = dispatch_get_main_queue (); 2 3 dispatch_sync (queue, ^{45// download files, pictures etc Resources 6 7 });
The home row Synchronous commit task is bound to deadlock , that is, the thread is blocked, and no further code execution continues
In particular, when using synchronous commits, it is not possible to submit a task to this thread in the current thread
The main thread executes task A, adds task B to the primary queue in task A, and task B executes in the main thread, and because the home column is a serial queue , the task executes sequentially, task a finishes executing task B, but task A is executed and task B must be executed, but is task B waits for task A to execute before it executes, so a deadlock occurs and the code does not execute down
Primary queue Asynchronous Commit
1 dispatch_queue_t queue = dispatch_get_main_queue (); 2 3 dispatch_async (queue, ^{45// download files, pictures etc Resources 6 7 });
This situation is also very special, although it is asynchronous commit, but no new thread to open, GCD will be at the right time to put the task you submitted in the main thread, execution time is not controlled
Other uses of GCD
Dispatch_after: Deferred execution
1 dispatch_time_t time = Dispatch_time (Dispatch_time_now, (int64_t) (Requires delay * nsec_per_sec)); 2 3 Dispatch_after (Time, Dispatch_get_main_queue (), ^{45// Code 67 } that requires deferred execution);
It is important to note that this is not a time to perform a task, but to submit the task to the queue
1NSLog (@"Thread Execution Start");2 3Dispatch_async (Dispatch_get_main_queue (), ^{4 5[Nsthread Sleepfortimeinterval:Ten.];6 7 });8 9dispatch_time_t time = Dispatch_time (Dispatch_time_now, (int64_t) (5. *nsec_per_sec));Ten OneDispatch_after (Time, Dispatch_get_main_queue (), ^{ A -NSLog (@"deferred submission of tasks "); - the });
Execution results
The second print here is 10 seconds after the first print, not 5 seconds later
Dispatch_once: Executes only one piece of code at a time
1 Static dispatch_once_t Oncetoken; 2 3 dispatch_once (&oncetoken, ^{45// code 6 to execute only once 7 });
It is important to note that Oncetoken must use the static declaration , in order to ensure that the code executes once, otherwise, the code can not be guaranteed to execute only once, there will be difficult to fix the bug
Dispatch_group
Usage Scenario: When you perform multiple asynchronous tasks and wait until all tasks have finished performing certain operations
1dispatch_group_t Group =dispatch_group_create ();2 3Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{4 5 //Submit Task A6 7 });8 9Dispatch_group_async (Group, Dispatch_get_global_queue (Dispatch_queue_priority_default,0), ^{Ten One //Submit Task B A - }); - theDispatch_group_notify (Group, Dispatch_get_main_queue (), ^{ - - //after all tasks have been executed, the required actions - +});
Here are two ways to notify all tasks to complete:
- Dispatch_group_notify This is an asynchronous notification that does not block the current thread (common)
- Dispatch_group_wait This one will wait until all tasks are completed or timed out
Dispatch_barrier_sync and Dispatch_barrier_async
1 0), ^{23// Submit Task 45 });
The tasks submitted by these two functions, when executed, will block the subsequent tasks , during which time only this task is executed, and subsequent tasks will not be executed until the task is completed, and all tasks prior to this task must be completed before the task.
Dispatch_barrier_sync and Dispatch_barrier_async only work on the concurrent queues that they create , in global concurrent queues, on serial queues, with Dispatch_sync, Dispatch_ Same as Async effect
GCD Usage Summary