Basic idea: Put the task (action) in the queue to execute
Task using block definition
The queue is responsible for scheduling the thread on which the task executes and the specific execution time
Queues are characterized by FIFO, and new additions to the queue are queued at the end of the team.
The functions of GCD are all beginning with dispatch.
Note: The queue is not a thread and does not correspond to the CPU.
Queue: dispatch_queue_t
Two types of queues: Serial queue, parallel queue
Actions on queues: adding tasks
Two kinds of tasks: synchronous tasks, asynchronous tasks
GCD serial queue, which means that tasks in the queue are queued for execution
1) Add an asynchronous task: Create a thread that all the added tasks are queued for execution
dispatch_queue_t q = dispatch_queue_create ("Teacheran", dispatch_queue_serial); for (int i=0; i<10; i++) {Dispatch_ Async (Q, ^{NSLog (@ "%@%d", [Nsthread CurrentThread], i); });}
2) Add Sync Task: Do not create thread, task executes in current thread, seldom use
dispatch_queue_t q = dispatch_queue_create ("Teacheran", dispatch_queue_serial);d Ispatch_sync (q, ^{NSLog (@ "%@%d", [NS Thread CurrentThread], i);});
CGD parallel queues means that tasks in the queue are not queued and executed randomly
1) Add asynchronous task: Create thread, indeterminate number, indeterminate task order
dispatch_queue_t q = dispatch_queue_create ("Teacheran", Dispatch_queue_concurrent);d Ispatch_async (q, ^{NSLog (@ "%@%d ", [Nsthread CurrentThread], i);});
2) Add Sync Task: No threads are created, very few use
dispatch_queue_t q = dispatch_queue_create ("Teacheran", Dispatch_queue_concurrent);d Ispatch_sync (q, ^{NSLog (@ "%@%d" , [Nsthread CurrentThread], i);});
No matter what the queue or task, the creation and recycling of threads does not require programmer involvement, and thread creation and recycling is the responsibility of the queue
The most common queue for GCD is the global queue and the home column, which do not need to be created, and are the same for every app
Get Global Queue
dispatch_queue_t Dispatch_get_global_queue (long identifier, unsigned long flags);
Flags parameter: Preserve parameters, pass 0
Identifier parameter: Used to specify which global queue to use
Four global queues:
#define DISPATCH_QUEUE_PRIORITY_HIGH 2//high-priority
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0//Default priority
#define Dispatch_queue_priority_low (-2)//Low priority
#define Dispatch_queue_priority_background int16_min//Lowest Background priority
The global queue is a system, which can be used directly, all parallel queues.
Adding an asynchronous task creates multiple threads and the task does not need to be executed
Add synchronization task, do not create new thread, task sort execution
Each application has only one main thread, and in iOS development, all UI updates must be done in the main thread
Get the home row:
dispatch_queue_t dispatch_get_main_queue (void);
The main queue is also the system, take it directly, add the task will terminate the main thread hi
Adding asynchronous tasks: Sequential execution in the main thread, no-step effect
Add Synchronization Task: task does not execute
Common usage:
Add an asynchronous task using a global queue if a task wants to execute in the background (child thread)
If a task wants to execute on the main thread, use the primary queue to add the synchronization task
This is the most common practice of requesting data in a child thread, and then updating the relevant UI elements back to the main thread after completion
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7C/B0/wKiom1bWTgPwJHq_AAGUwDqRo34291.png "title=" screen shot 2016-03-02 a.m. 10.20.56.png "alt=" Wkiom1bwtgpwjhq_aaguwdqro34291.png "/>
1) Delayed execution of tasks
The Dispatch_after function delays adding a block task to the queue
void Dispatch_after (dispatch_time_t when, dispatch_queue_t queue, dispatch_block_t block);
In Xcode, complete the code by using the shortcut key:
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7C/AF/wKioL1bWTsnj5Y2lAACHwjf_uQo078.png "title=" screen shot 2016-03-02 a.m. 10.22.24.png "alt=" Wkiol1bwtsnj5y2laachwjf_uqo078.png "/>
Delayinseconds: Number of seconds to delay
Use of the primary queue, that is, deferred tasks are performed in the main thread
The last block, the task code
2) Multiple executions of the task
The Dispatch_apply function puts a task into the queue multiple times
Whether serial execution or parallel execution is determined by the specific queue.
Dispatch_apply does not return immediately, returns after execution, is a synchronous call
void Dispatch_apply (size_t iterations, dispatch_queue_t queue, void (^block) (size_t));
Iteractions: Number of executions
Queue: Queues, general use of global queues
Block: Task code
Scenario: Putting a tight-knit loop operation in the background
3) Ensure that the task is executed only once
In the multi-threaded environment, with the Forbidden City Dispatch_once () to ensure that a task will only be executed once
In Xcode, complete the code by using the shortcut key:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7C/AF/wKioL1bWTwzB9dHJAABfRZrWsE8675.png "title=" screen shot 2016-03-02 a.m. 10.23.29.png "alt=" Wkiol1bwtwzb9dhjaabfrzrwse8675.png "/>
Add the task code to the block
Scenario: Creating a Singleton object
This article is from the "Teacheran" blog, make sure to keep this source http://annmeng.blog.51cto.com/3321237/1746609
Basic use of iOS multi-thread GCD