ios-thread GCD Mode---the relationship between synchronous asynchronous and serial queue parallel queues

Source: Internet
Author: User
Tags gcd

GCD Way is Apple's official recommendation to implement multithreading

But before this, we must understand the concept of synchronous, asynchronous, serial queue, parallel queue.

synchronization: The current execution block does not create a new thread and can only be executed in the current thread.

Async: creates a new thread outside the current thread and executes a block of code in the new threads.

First of all, the queue is used to store the body of the thread that is about to execute.

Serial queue: the thread in the serial queue satisfies the FIFO (first in first out), and only the preceding thread executes, and subsequent threads can execute out of the queue. (It is likely to cause the app's suspended animation)

parallel queues: parallel queues also meet FIFO, which can be implemented simultaneously with parallel queues and asynchronous methods (as described later)

The following four scenarios in the Code section

1. Serial Queue and synchronization mode (deprecated)

1 //1. Synchronizing the code to the serial queue2 //after the previous thread finishes executing, the subsequent thread will not begin executing3- (void) Testsyncwithserialqueue4 {5     //Serial Queue6dispatch_queue_t Serialqueue = Dispatch_queue_create ("Serialqueue", dispatch_queue_serial);7     8     //Thread One9Dispatch_sync (Serialqueue, ^ (void){Ten          for(intI=0; i< -; i++) { OneNSLog (@"line Cheng:%d", i); A         } -     }); -      the     //thread Two -Dispatch_sync (Serialqueue, ^ (void){ -          for(intI=0; i< -; i++) { -NSLog (@"thread Two:%d", i); +         } -     }); +}

Execution results and conclusion: after the execution of the previous thread is completed , the subsequent thread is not started ---

2015-08-08 19:59:35.730 testasyncandsync[3187:141197] Thread One :

2015-08-08 19:59:35.730 testasyncandsync[3187:141197] Thread One :

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] Thread One : 98

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] Thread One :

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] thread two : 0

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] thread Two : 1

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] thread Two : 2

2015-08-08 19:59:35.731 testasyncandsync[3187:141197] thread Two : 3

2. Serial Queue and asynchronous mode

1- (void) Testasyncwithserialqueue2 {3dispatch_queue_t Serialqueue = Dispatch_queue_create ("Serialqueue", dispatch_queue_serial);4     //Async, Thread one5Dispatch_async (Serialqueue, ^ (void){6          for(intI=0; i< -; i++) {7NSLog (@"Thread 1:%d", i);8         }9     });Ten      One     //Async: Thread Two ADispatch_async (Serialqueue, ^ (void){ -          for(intI=0; i< -; i++) { -NSLog (@"Thread 2:%d", i); the         } -     }); -}

Execution results and conclusion: the thread in front of the serial queue executes to perform the subsequent thread

2015-08-08 20:03:28.391 testasyncandsync[3225:143307] thread 1:97

2015-08-08 20:03:28.391 testasyncandsync[3225:143307] thread 1:98

2015-08-08 20:03:28.391 testasyncandsync[3225:143307] thread 1:99

2015-08-08 20:03:28.391 testasyncandsync[3225:143307] thread 2:0

2015-08-08 20:03:28.391 testasyncandsync[3225:143307] thread 2:1

2015-08-08 20:03:28.392 testasyncandsync[3225:143307] thread 2:2

3. Parallel queues and synchronous creation of threads

1- (void) Testsyncwithconcurrentqueue2 {3dispatch_queue_t Concurrentqueue = Dispatch_queue_create ("Concurrentqueue", dispatch_queue_concurrent);4     //Thread One5Dispatch_sync (Concurrentqueue, ^ (void){6          for(intI=0; i< -; i++) {7NSLog (@"line Cheng:%d", i);8         }9     });Ten      One     //Thread One ADispatch_sync (Concurrentqueue, ^ (void){ -          for(intI=0; i< -; i++) { -NSLog (@"thread Two:%d", i); the         } -     }); -}

Results and conclusion: line Cheng is executed first, then thread two is executed.

2015-08-08 20:06:19.111 testasyncandsync[3257:144554] Thread One :

2015-08-08 20:06:19.111 testasyncandsync[3257:144554] Thread One : 98

2015-08-08 20:06:19.111 testasyncandsync[3257:144554] Thread One :

2015-08-08 20:06:19.112 testasyncandsync[3257:144554] thread two : 0

2015-08-08 20:06:19.112 testasyncandsync[3257:144554] thread Two : 1

2015-08-08 20:06:19.112 testasyncandsync[3257:144554] thread Two : 2

4. Parallel Queue and asynchronous mode

1- (void) Testasyncwithconcurrentqueue2 {3dispatch_queue_t Concurrentqueue = Dispatch_queue_create ("Concurrentqueue", dispatch_queue_concurrent);4     //Async, Thread one5Dispatch_async (Concurrentqueue, ^ (void){6          for(intI=0; i< -; i++) {7NSLog (@"Thread 1:%d", i);8         }9     });Ten      One     //Async: Thread Two ADispatch_async (Concurrentqueue, ^ (void){ -          for(intI=0; i< -; i++) { -NSLog (@"Thread 2:%d", i); the         } -     }); -      -}

Execution result and conclusion: line Cheng and line Cheng are executed simultaneously

2015-08-08 20:07:48.318 testasyncandsync[3283:145696] thread 1:0

2015-08-08 20:07:48.318 testasyncandsync[3283:145693] thread 2:0

2015-08-08 20:07:48.319 testasyncandsync[3283:145693] thread 2:1

2015-08-08 20:07:48.319 testasyncandsync[3283:145693] thread 2:2

2015-08-08 20:07:48.319 testasyncandsync[3283:145693] thread 2:3

2015-08-08 20:07:48.319 testasyncandsync[3283:145696] thread 1:1

Explain the above four cases: have to say, thread queue is a magical thing, it seems uncontrollable, and there are traces to follow ...

Why does it cause four of the above cases:

First of all, in the fourth case, asynchronously creates a thread and uses a parallel queue to store it. The main reason is that the parallel queue allows the queue to be out of the queue immediately after the thread is out of the queue. Instead of using asynchronous methods, Each out-of-the-thread experience creates a new thread that executes the corresponding block of code. Therefore, two threads are executed at the same time. (but there are also those who make you feel powerless, you can not fully control two threads, even if first out of the queue thread, it is not necessarily finished first, the speed of the thread execution is mainly due to two threads preemption system resources to obtain how much)

First and second case: Because the serial only allows advanced queue of threads, after the queue after the execution to the next thread out of the queue, so must be ordered (according to the queue in order)

The third case: Parallel synchronization: Although the parallel queue allows multiple threads to be out of the queue at the same time (or FIFO), the first-out thread does not create a new thread, only executes the task in the current thread, because only one of the current threads, all of which are out of the queue at the same time, can only be waited for.

ios-thread GCD Mode---the relationship between synchronous asynchronous and serial queue parallel queues

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.