ios--process/multithreaded/Synchronous Task/Asynchronous task/serial queue/parallel queue (contrast analysis)

Source: Internet
Author: User
Tags gcd

Now say the first two basic concepts, what is the process, what is a thread, what is multi-threaded; Let's get these two to the bottom of the story.

Process: An ongoing program, we call it process.

Thread: A thread is a separate execution path in a process. How to understand this sentence! A program it is executed sequentially from top to bottom, this execution order we can think of it as a line, the line is called the thread (personal understanding, wrong do not spray); Each program contains at least one thread, this thread, we call it the main thread.

Multithreading: Multithreading means that there are multiple execution paths in a program. In iOS, some of the more time-consuming operations are placed in another execution path. Let it run concurrently with the main thread. This does not cause the main thread to clog.

We'll see what the task is, what's the queue!

Tasks are divided into synchronous and asynchronous tasks, and the differences are:
The synchronization task has a high priority, there is a sequence of executions in the thread, and no new threads are opened.
Asynchronous task priority is low, execution in thread is not sequential, see CPU idle. New threads are not opened in the primary queue, and other queues open new threads.

Queue: Queue We can think of it as a management task, with a lot of tasks in place to manage when these tasks are executed in which threads. Queue is FIFO

Queues are divided into serial queues and parallel queues

Serial queue: The tasks in the queue are executed sequentially (not concurrently)
Parallel queues: The tasks in the queue are executed concurrently, and there may be a question that the queue is not FIFO, and if the later tasks are finished, how to get out. It needs to be stressed here that the task is done, not necessarily out of the queue. The queue will only be out if the previous task is done.

Serial queues open asynchronous tasks, which are sequential

synchronous tasks in parallel queues are executed sequentially, only asynchronously.

To regaling to open a synchronization task in the main queue why is the thread blocked?

The synchronization task is turned on in the main queue because the home column is a serial queue, the threads inside are sequential, the next thread executes after executing a thread, and the host column always has only one main thread, and the main thread is not executed because he is an infinite loop, unless the application is closed. Therefore, in the main thread to open a synchronization task, the synchronization task will want to preempt the execution of resources, and the main thread task has been performing certain operations, refused to let go. Two priority levels are high, resulting in deadlock, blocking threads

Synchronization cannot be turned on in the main thread queue

Synchronization cannot be turned on in the main thread queue, blocking the main thread. Only the asynchronous task can be turned on, and the asynchronous task will not open the new thread, just reduce the priority of the asynchronous task, let the CPU idle time to call. synchronous tasks, which preempt the main thread's resources, can cause deadlocks.

Why does a nested synchronization task cause a deadlock when a serial queue turns on an asynchronous task? (You will not die without dying)

Because the serial queue threads are in order of execution, the synchronization tasks that are opened below are performed only if the asynchronous task that is opened above is completed. and the above asynchronous task is not finished, to the following curly brace is executed, and the following synchronization task is already preemption resources, there will be deadlock.


A nested synchronization task causes a deadlock when a synchronization task is turned on in the serial queue (a few more, you don't know how to die)?

Because the serial queue threads are in the order of execution, the synchronization tasks that are turned on are performed only when the synchronization task that is opened above is completed. and the above synchronization task is not finished, to the following curly brace is executed, and the following synchronization task is already preemption resources, there will be deadlock

A serial queue starts a synchronization task and the nested asynchronous task does not cause a deadlock, opening the asynchronous, opening a new thread, not blocking the thread

Is there a difference between the main thread queue and the queue created by GCD?

The main thread queue and the queue created by GCD are different. The queue priority created in GCD does not have a home column height, so there is no nested task inside the serial queue in GCD that does not block the main thread, only one of which could lead to deadlocks, which is the serial queue, nested open tasks that could lead to deadlocks.

ios--process/multithreaded/Synchronous Task/Asynchronous task/serial queue/parallel queue (contrast analysis)

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.