"iOS Interview Series-2" the logical relationship between synchronous, asynchronous, and serial, parallel in multi-threading (required, must be mastered)

Source: Internet
Author: User
Tags gcd

One, synchronous, asynchronous and serial, parallel

Task serial execution means that only one task is executed at a time, and the concurrent execution of the task is that multiple tasks can be executed at the same moment.

a synchronization function is returned only after it has completed its scheduled task. OneAsynchronous Functions, just the opposite, will return immediately, the scheduled task will be completed but will not wait for it to complete.  Therefore, an asynchronous function does not block the current thread from executing the next function. (Source: http://www.cocoachina.com/industry/20140428/8248.html)

Queues are divided into serial and parallel

Task execution is divided into synchronous and asynchronous

-------Queue is only responsible for scheduling tasks, not for task execution---------

-------task is performed in the thread---------

Serial queue: The task is dispatched in order, the previous task is not completed, and the queue is not dispatched

Parallel queue: As long as there are idle threads, the queue will dispatch the current task, to the thread to execute, do not need to consider the previous task is executed, as long as the thread can be exploited, the queue will dispatch the task.

Synchronous execution: New threads are not opened, tasks are executed sequentially

Asynchronous execution: A new thread is opened and the task can be executed concurrently

(Source: http://www.cnblogs.com/KongPro/p/5193175.html)

"Note: Two people say there is contradiction, a said queue to perform tasks, one said the queue does not perform the task = = a synchronous asynchronous is the return problem, a synchronous asynchronous is not open thread problem"   同步(sync) 异步(async) The main difference is whether the current thread will be blocked until Block the task in the execution is complete!
if it is an 同步(sync) operation, it blocks the current thread and waits Block for the task to complete before the current thread continues to run down.
if it is an 异步(async) operation, the current thread executes directly down, and it does not block the current thread. (original link: http://www.jianshu.com/p/0b0d9b1f1f19)"The third person has the same meaning as the first person."

Second, the queue

GCD provides dispatch queues to handle code blocks that manage the tasks you provide to GCD and perform these tasks in a FIFO order. This guarantees that the first task to be added to the queue will be the first task in the queue, and the second task to be added will start the second one, so that the end of the queue.

1. Serial Queue

The only thing that can be ensured is that GCD executes only one task at a time and executes in the order that we add to the queue. Because there are not two tasks running concurrently in the serial queue.

  Put the task into the serial queue, GCD takes FIFO(先进先出) out one, executes one, and then takes the next one, such a execution.

2. Concurrent Queues

The tasks in the concurrent queues are guaranteed that they will be executed in the order in which they were added, that the task may be completed in any order, that you will not know when to start the next task, or how many blocks are running at any one time. Again, it all depends on GCD.

    In parallel queue tasks, GCD will also be FIFO taken out, but the difference is that it takes out one of the other threads and then pulls out another thread. As a result of taking the action quickly, negligible, it seems that all the tasks are executed together. However, it is important to note that GCD will control the number of parallelism based on system resources, so if there are many tasks, it will not allow all tasks to execute concurrently.

(Source: http://www.cocoachina.com/industry/20140428/8248.html)

  (Original link: http://www.jianshu.com/p/0b0d9b1f1f19)

Three, the combination

    1. Serial Queue Synchronous Execution: synthesizes the features of the serial queue described above---sequential, synchronous: No new threads are opened , then serial queue synchronization is performed only by step one by one.
    2. Serial Queue Asynchronous execution : Although the queue is the asynchronous execution of the task, but combined with the characteristics of the serial queue, the previous task does not complete, the queue will not be dispatched , so the serial queue asynchronous execution is also one by one execution
    1. Parallel Queue Synchronous execution : combined with the characteristics of the parallel queue described above, and the characteristics of synchronous execution, can be clearly analyzed, although the parallel queue can not wait for the previous task to complete the next task can be dispatched, but task synchronization does not open a new thread , So the task is also the one by one execution
    2. Parallel queue asynchronous execution: The next one describes the characteristics of the parallel queue, and asynchronous execution is the task can open a new thread, so this combination can achieve the concurrency of the task, and then the actual development is often used

(Source: http://www.cnblogs.com/KongPro/p/5193175.html)

"Personal Summary: serial , parallel difference is that there is a task in the execution of the new task can be carried out

        Synchronous , asynchronous differences will not block the current thread

Serial synchronization: takes out a task , blocks the current thread , executes the task, completes the task , and the next task

Serial async: Takes out a task , does not block the current thread , waits for the task to complete , the task finishes, and the next task

Parallel synchronization: Takes out a task , blocks the current thread , executes the task , waits for the task to finish , and the next task

Parallel async: Takes out a task , does not block the current thread , waits for the task to complete, not wait for the task to complete , the next task

"So the big question now is, what if there's only one thread going to execute the task asynchronously in a parallel queue?" In the sense of the word, not the thread is synchronous, once called asynchronous to explain the opening of the thread, so the three people summed up is more reasonable "

Let queue = Dispatch_queue_create ("Myqueue",dispatch_queue_serial)//Create serial queue    NSLog ("Before-%@",Nsthread.currentthread ()) Dispatch_async (queue, {())VoidInch// asynchronous serial queue execution- open a new thread    nslog ( "sync before-%@",  Nsthread.currentthread ()) Dispatch_sync (queue, {(), void in // synchronous execution   serial queue   - and wait for  (while the previous task was blocking this)      NSLog ( "sync-%@", nsthread.currentthread ())}) nslog (" after sync-%@ ", nsthread.currentthread ())}) Span class= "Hljs-type" >nslog ( "after-%@", nsthread.currentthread () )

"On the blog to find or not rest assured that there is time in the book to see the relevant I will come back to update"

"iOS Interview Series-2" the logical relationship between synchronous, asynchronous, and serial, parallel in multi-threading (required, must be mastered)

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.