< turn >ios-chart and Mao, hand teach you GCD

Source: Internet
Author: User

This article goes from:http://www.jianshu.com/p/414b8e91e021Objective

For beginners, GCD seems to be a step in the past, a lot of people in the synchronization, asynchronous, serial, parallel and deadlock in the vortex of the few nouns gradually give up treatment. This article will use the graphic table and Mao's way to give you an image of the principles and laws of interpretation.

Concepts of threads, tasks, and queues
Asynchronous, synchronous & parallel, serial features
An important guideline.

In general, the maximum purpose of our use of GCD is to perform multiple tasks concurrently in a new thread , which means that we need two conditions:

    • To open a new thread
    • Tasks can be executed at the same time
    • Combined with the above two conditions, it is equivalent to "the ability to open a new thread + task synchronous execution of the right", only when the ability and rights to meet the conditions of the premise, we can perform multiple tasks at the same time.
Features of all combinations
(i) Asynchronous execution + parallel queue implementation code:
//asynchronous execution + parallel queue- (void) asyncconcurrent{//Create a parallel queuedispatch_queue_t queue = Dispatch_queue_create ("identifiers", dispatch_queue_concurrent); NSLog (@"---start---"); //encapsulating three tasks with an asynchronous functionDispatch_async (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
  ---start---  ---end---   task 30x600000070f005, name = (null)}  Task 2 0x600000070d80 4, name = (null)}  Task 10x6080000741003, name = (null )}
Explain
    • Asynchronous execution means that the
      • Can open a new thread
      • The task can be bypassed, not executed, back to execution.
    • A parallel queue means
      • There is no need to queue between tasks, and there is a "right" that is executed simultaneously
    • The result after the combination of the two
      • Three new threads were opened
      • When the function is executed, start and end are printed, and then the three tasks are taken back.
      • These three tasks are executed at the same time, not successively, so the print result is "Task 3--> task 2--> 1"
Step diagram
(b) Asynchronous execution + serial queue implementation code:
//asynchronous execution + serial queue- (void) asyncserial{//Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("identifiers", dispatch_queue_serial); NSLog (@"---start---"); //encapsulating three tasks with an asynchronous functionDispatch_async (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
---start------end--- task 10x6080000784803, name = (null)} Task 2  0x6080000784803, name = (null)} Task 30x6080000784803, name = (  Null)}
Explain
    • Asynchronous execution means that the
      • Can open a new thread
      • The task can be bypassed, not executed, back to execution.
    • A serial queue means
      • Tasks must be executed in the order in which they are added to the queue
    • The result after the combination of the two
      • A new sub-thread was opened
      • When the function is executed, start and end are printed, and then the three tasks are taken back.
      • These three tasks are executed sequentially, so the print result is "Task 1--> task 2--> Task 3"
Step diagram
(c) Synchronous execution + parallel queue implementation code:
//synchronous execution + parallel queue- (void) syncconcurrent{//Create a parallel queuedispatch_queue_t queue = Dispatch_queue_create ("identifiers", dispatch_queue_concurrent); NSLog (@"---start---"); //encapsulate three tasks with a sync functionDispatch_sync (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
  ---start---   task 10x6080000654001, name = main}  Task 20x608000065400  1, name = Main}  task 30x6080000654001, name = main}  - --end---
Explain
    • Executing execution synchronously means that the
      • Unable to open new thread
      • The task must be completed after it is created to go down
    • A parallel queue means
      • There is no need to queue between tasks, and there is a ""
    • The result after the combination of the two
      • All tasks can only be performed in the main thread
      • When the function executes, it must be executed one line at a time in the code's writing order to continue
    • Precautions
      • In this case, even if the parallel queue, the task can be executed at the same time, but because there is only one main thread, so the task cannot be distributed to different threads to synchronize processing, the result is only in the main line thread in order to execute the
Step diagram
(d) Synchronous execution + serial queue implementation code:
- (void) syncserial{//Create a serial queuedispatch_queue_t queue = Dispatch_queue_create ("identifiers", dispatch_queue_serial); NSLog (@"---start---"); //encapsulating three tasks with an asynchronous functionDispatch_sync (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
---start---   task 10x6080000654001, name = main}  Task 20x608000065400  1, name = Main}  task 30x6080000654001, name = main}  -- -end---
Explain
    • The principle of execution and the step diagram here is the same as "synchronous execution + concurrent queue", as long as synchronous execution can not open a new thread, so the multiple tasks can only be executed sequentially,
(v) Asynchronous execution + Home column implementation code:
- (void) asyncmain{//get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); NSLog (@"---start---"); //encapsulating three tasks with an asynchronous functionDispatch_async (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_async (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
---start---  ---end---   task 10x60800006ff401, name = main}  task 2  0x60800006ff401, name = Main}  task 30x60800006ff401, name = main}
Explain
    • Asynchronous execution means that the
      • Can open a new thread
      • The task can be bypassed, not executed, back to execution.
    • The difference between a master queue and a serial queue
      • Tasks in the queue are executed sequentially
      • The tasks in the master queue must be executed in the main thread, not allowed in the child threads
    • The above conditions are combined to produce the result:
      • All tasks can be skipped first, followed by "sequential" execution
Step diagram
(vi) Synchronous execution + home row (deadlock) Implementation code:
- (void) syncmain{//get the home rowdispatch_queue_t queue =Dispatch_get_main_queue (); NSLog (@"---start---"); //encapsulate three tasks with a sync functionDispatch_sync (Queue, ^{NSLog (@"Task 1---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 2---%@", [Nsthread CurrentThread]);    }); Dispatch_sync (Queue,^{NSLog (@"Task 3---%@", [Nsthread CurrentThread]);    }); NSLog (@"---End---");}
Printing results:
---start---
Explain
    • Tasks in the master queue must be executed sequentially
    • Task 1 does not execute until the main thread is empty (that is, all tasks in the primary queue are executed)
    • The main thread will not be empty until the "Print end" task is executed
    • "Task 1" and "Print End" two tasks waiting for each other, resulting in a deadlock
Step diagram
Write it at the end.

The above is my basic knowledge of GCD and several combinations of understanding, if I feel that my blog is also able to write, welcome to follow my blog, I will be long-term for you to launch high-quality technology blog. Of course, if you think I understand what is wrong, you can leave your comments.

Wen/aaron_zhangkh (author of Jane's book)
Original link: http://www.jianshu.com/p/414b8e91e021
Copyright belongs to the author, please contact the author to obtain authorization, and Mark "book author".

< turn >ios-chart and Mao, hand teach you GCD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.