1- deferred execution
can schedule its thread (1), the home row
dispatch_queue_t queue= dispatch_get_main_queue ();
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec)), queue, ^{
NSLog (@ "Home row - deferred execution ------%@", [Nsthread CurrentThread]);
});
can schedule its threads (2), concurrent queues
1. Get global concurrent queues
dispatch_queue_t queue1= dispatch_get_global_queue (Dispatch_queue_priority_default, 0);
2. Calculate time for task execution
dispatch_time_t When=dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec));
3. The task in the queue will be executed at this point in time
Dispatch_after (when, queue1, ^{
NSLog (@ " concurrent Queue - deferred execution ------%@", [Nsthread CurrentThread]);
});
2. use dispatch_once Disposable code
Use the Dispatch_once function to ensure that a piece of code is executed only 1 times during program Operation
Static dispatch_once_t Oncetoken;
Dispatch_once (&oncetoken, ^{
Execute code only 1 times ( This is thread-safe by default )
});
The entire program runs, only once.
3. Queue Groups
The use of queue groups allows task 1 and Task 2 to be performed simultaneously, and when both tasks are completed, return to the main thread for interface display operations.
1. Create a queue group
dispatch_group_t group = dispatch_group_create();
2. Start a task
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ /c15>
});
3. Open a second task
dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{
});
perform task 1\ Task 2 Operations simultaneously
4. All tasks in the group are completed, and then go back to the main thread to perform other operations
dispatch_group_notify(group,Dispatch_get_main_queue(), ^{
});
iOS multithreaded Development gcd common usage