IOS-GCD detailed and simple use _ios

Source: Internet
Author: User
Tags gcd

IOS-GCD Introduction

During the development process, we sometimes want to encapsulate some of the operations and delay them for a while. in iOS development, there are two common ways to implement deferred execution, one is to use GCD, and the other is to use the methods provided in the Nsrunloop class.

Objective

For beginners, GCD seems to be a barrier to the past, many people in the synchronous, asynchronous, serial, parallel and deadlock in the vortex of several nouns gradually abandoned treatment. This article will use the graphic form and Mao's way to give you an image of the principles and rules.

Concepts of threads, tasks, and queues

Asynchronous, synchronous & parallel, serial features

An important rule.

In general, the biggest goal of using GCD is to execute multiple tasks simultaneously in a new thread, which means we need two conditions:

    1. Can open a new thread
    2. Tasks can be executed concurrently
    3. Combined with the above two conditions, it is also equivalent "the ability to open a new thread + the right to execute the task synchronously", only in the context of the ability and rights to meet the two conditions, we can perform multiple tasks at the same time.

The characteristics of all combinations

(i) asynchronous execution + parallel queues

Implementation code:

Asynchronous execution + Parallel queue
-(void) asyncconcurrent{
  //Create a parallel queue
  dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_concurrent);
 
  NSLog (@ "---start---");
 
  Use asynchronous functions to encapsulate three tasks
  dispatch_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---");
}

Print results:

---start---

  ---end---

  任务3---{number = 5, name = (null)}

  任务2---{number = 4, name = (null)}

  任务1---{number = 3, name = (null)}

Explain:

1. Asynchronous execution means

Can open a new thread

Tasks can be bypassed without execution, and then executed later.

2. Parallel queues Mean

There is no need to queue between tasks and have "rights" that are executed concurrently

3. Results after combination of both

Three new threads were opened

When the function is executed, the start and end are printed first, and then the three tasks are performed.

These three tasks are executed at the same time, not successively, so the print result is "Task 3--> task 2--> Task 1"

Step diagram

(ii) asynchronous execution + serial queue

Implementation code:

Asynchronous execution + Serial queue
-(void) asyncserial{
  //Create a serial queue
  dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_serial);
 
  NSLog (@ "---start---");
  Use asynchronous functions to encapsulate three tasks
  dispatch_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---");
}

Print results:

 ---start---

 ---end---

任务1---{number = 3, name = (null)}

任务2---{number = 3, name = (null)}

任务3---{number = 3, name = (null)}

Explain:

Asynchronous execution means that the

Can open a new thread

Tasks can be bypassed without execution, and then executed later.

A serial queue means

Tasks must be executed in the order that they are added into the queue

The results of both combinations

A new child thread was opened

When the function is executed, the start and end are printed first, and then the three tasks are performed.

These three tasks are performed sequentially, so the print result is "Task 1--> task 2--> Task 3"

Step diagram

(iii) synchronous execution + parallel queues

Implementation code:

Synchronous execution + Parallel queue
-(void) syncconcurrent{
  //Create a parallel queue
  dispatch_queue_t \ dispatch_queue_create ("identifier", dispatch_queue_concurrent);
 
  NSLog (@ "---start---");
  Encapsulates three task
  Dispatch_sync (the queue, ^{
    NSLog (@ Task 1---%@, [nsthread CurrentThread]) using
  a synchronization function;
  Dispatch_sync (queue, ^{
    NSLog (@ "Task 2---%@", [Nsthread CurrentThread]);
  Dispatch_sync (queue, ^{
    NSLog (@ "Task 3---%@", [Nsthread CurrentThread]);
  NSLog (@ "---end---");
}

Print results:

---start---

  任务1---{number = 1, name = main}

  任务2---{number = 1, name = main}

  任务3---{number = 1, name = main}

  ---end---

Explain:

Executing execution synchronously means that the

Unable to open new thread

After the task is created, you must finish it before you go down.

A parallel queue means

Tasks must be executed in the order that they are added into the queue

The results of both combinations

All tasks can only be performed in the main thread

When a function executes, it must be executed one line at a time according to the writing order of the Code to continue

Attention matters

In this case, even in parallel queues, tasks can be executed at the same time, but because there is only one main thread, it is not possible to distribute the task to different threads to synchronize, the result is only in the main line Chengri in order to execute the

Step diagram

(iv) synchronous execution + serial queue

Implementation code:

-(void) syncserial{
  //Create a serial queue
  dispatch_queue_t \ = dispatch_queue_create ("identifier", dispatch_queue_serial) ;
 
  NSLog (@ "---start---");
  Use asynchronous functions to encapsulate three tasks
  dispatch_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---");
}

Print results:

  ---start---

  任务1---{number = 1, name = main}

  任务2---{number = 1, name = main}

  任务3---{number = 1, name = main}

  ---end---

Explain:

The implementation principle and step diagram here is the same as "synchronous execution + concurrent queue", so as long as it is executed synchronously, it is impossible to open new threads, so the multiple tasks can be executed in sequence only.

(v) Asynchronous execution + home team column

Implementation code:

-(void) asyncmain{
  //Get home team column
  dispatch_queue_t queue = Dispatch_get_main_queue ();
 
  NSLog (@ "---start---");
  Use asynchronous functions to encapsulate three tasks
  dispatch_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---");
}

Print results:

  ---start---

  ---end---

  任务1---{number = 1, name = main}

  任务2---{number = 1, name = main}

  任务3---{number = 1, name = main}

Explain

Asynchronous execution means that the

Can open a new thread

Tasks can be bypassed without execution, and then executed later.

The difference between a master queue and a serial queue

Tasks in the queue are executed in the same order

Tasks in the primary queue must be executed in the main thread and not allowed in child threads

The above conditions are combined to produce results:

All tasks can be skipped first, followed by the "in order" execution

Step diagram

(vi) Synchronous execution + home row (deadlock)

Implementation code:

-(void) syncmain{
  //Get home team column
  dispatch_queue_t queue = Dispatch_get_main_queue ();
 
  NSLog (@ "---start---");
  Encapsulates three task
  Dispatch_sync (the queue, ^{
    NSLog (@ Task 1---%@, [nsthread CurrentThread]) using
  a synchronization function;
  Dispatch_sync (queue, ^{
    NSLog (@ "Task 2---%@", [Nsthread CurrentThread]);
  Dispatch_sync (queue, ^{
    NSLog (@ "Task 3---%@", [Nsthread CurrentThread]);
  NSLog (@ "---end---");
}

Print results:

  ---start---

Explain

    1. Tasks in the master queue must be executed sequentially
    2. Task 1 to execute when the main thread is free (that is, all the tasks in the main queue are finished)
    3. The main thread will not be empty until the "Print End" task is performed
    4. Task 1 and Print end two tasks wait on each other, causing deadlocks

Step diagram

Thank you for reading, I hope to help you, thank you for your support for this site!

Related Article

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.