First, introduce several concepts: GCD, queue, serial, parallel, synchronous, asynchronous.
GCD (Grand Central Dispatch ):
The Great Apple developed a multi-core multi-thread solution.
- Is a C language-based underlying API
- Defining tasks with blocks is flexible and convenient to use.
- Provides more control capabilities and underlying functions not available in the Operation queue
The basic idea of GCD is to put the operation s in the queue s for execution.
- Use Blocks to define the operation
- The thread where the queue is responsible for scheduling task execution and the specific execution time
- The queue is characterized by first-in-first-out (FIFO). Newly Added to the column will be placed at the end of the team.
- All GCD functions start with dispatch (dispatch and scheduling ).
Queue (FIFO ):
The queue is an advanced, first-in-first-out organization. The most funny and vivid explanation I have seen is: (the difference between the queue and the stack is that if you pull too much, the queue is consumed too much and the stack is consumed too much ).
- In the serial queue, tasks in the queue are only executed sequentially.
- Parallel queue. Tasks in the queue are usually executed concurrently.
If the concept of "serial" and "Parallel" is unclear, I would like to give another example: 10 people have bought things in the supermarket and checked out in line at a cash register, the previous checkout operation is not completed, and the last one has to wait. This is a typical serial. 10 people go to 10 different cashier points to check the account at the same time. This is parallel. For more information, see the following example.
Synchronous and asynchronous (for more details, refer to the encyclopedia)
- Asynchronous operations (dispatch_async) are executed concurrently, and the execution sequence of tasks cannot be determined.
- The synchronization operation (dispatch_sync) is executed in sequence and can determine the execution sequence of tasks.
See the following four images:
Note: In parallel queues, the location of asynchronous tasks is meaningless. That is, asynchronous tasks are executed concurrently no matter where they are not placed!
Test source link: http://pan.baidu.com/s/1bnCd8HT
The above is purely personal understanding and has a limited level. If you have any mistakes, please try it!