iOS Multithreading summary-----GCD article

Source: Internet
Author: User

3 GCD (full use of the device's multi-box)-------------
The thread is blocked and only the task queue is visible
Step two steps, customize tasks, add tasks to the queue. GCD will add the task, put it into the thread to execute, automate, automatically release
Principle: First-out, first to add the task first to execute, different from the stack of advanced post-out, first add the task after the execution

-------GCD---

GCD has two functions to perform tasks: synchronous functions, asynchronous functions
Sync: Perform tasks sequentially, same thread execution (no threads)
Async: Executing a task concurrently (opening a new thread)

synchronous, asynchronous, concurrent, serial
Synchronization: The current thread executes without the ability to open a new thread
Async: Executes on a new thread with the ability to open a new thread
Concurrency: Multiple tasks are executed at the same time,
Serial: One task is completed and the next task is executed

<1> Create global queue
dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_default,0)
Note: Establish a global queue with priority default (queue scheduling frequency is default), followed by default 0, Apple official note, may be useful later,
Dispatch_async (queue,) ^{
NSLog (@ "---task 1---%@", [ Nsthread CurrentThread]);
}
Dispatch_async (queue,) ^{
NSLog (@ "---task 2---%@", [Nsthread CurrentThread]);
}
Dispatch_async (queue,) ^{
NSLog (@ "---task 3---%@", [Nsthread CurrentThread]);
}
Analysis: The decision whether to open the thread is the function async, asynchronous open a new thread, the global queue belongs to the concurrent queue, that is, the task can be executed concurrently, the execution scenario is, open three threads (not the main thread of three new threads), while performing three tasks.
If the function is sync, which is a synchronous function, synchronization does not open a new thread (that is, executing a task in the current thread), the concurrency of the home column is meaningless, the task is executed sequentially, so is the same as the serial

<2> Create a serial queue
dispatch_queue_t queue = dispatch_queue_creat ("Niubide.queue", NULL);
Note: The name of the serial queue in ""
Dispatch_async (queue,) ^{
NSLog (@ "---task 1---%@", [Nsthread CurrentThread]);
}
Dispatch_async (queue,) ^{
NSLog (@ "---task 2---%@", [Nsthread CurrentThread]);
}
Dispatch_async (queue,) ^{
NSLog (@ "---task 3---%@", [Nsthread CurrentThread]);
}
Analysis: Async is an asynchronous function, the new thread is opened, the serial queue is a non-concurrent queue, then the execution scheme is to open a new thread in this thread to perform the three-way task in turn
For synchronous functions, perform three tasks sequentially in the main thread

<3> Create a home column
dispatch_queue_t queue = Dispatch_get_main_queue ();
The primary queue is a special serial queue that performs tasks in the main thread regardless of synchronous asynchrony
Do not: If the synchronization method in the main thread will have a deadlock phenomenon, so the main thread requires the asynchronous home column to perform task operations
Application: Handles UI display issues in many sub-threads for processing time-consuming operations, creating the main thread for asynchronous display UI

GCD Summary:

Dispatch_async (Sync) (Global queue/home row/serial queue) block^{
}

Form----
Global Queue serial Queue home column

Sync does not open new thread does not open new thread does not open new thread
Task serial execution task serial execution tasks


Asynchronous open thread open thread does not open thread
Task concurrent Execute Task serial execution task serial execution


Other uses:
-Deferred call
contrast [self performselected: @selected (Run) Withobject:nil afterdealy:2.0]; Call the Run method when the front thread is delayed by 2 seconds

Dispatch_after (Dispatch_time_now, (int64_t) (3.0* nsec_per_sec), Dispatch_get_main_queue ())) ^{

}
Note: Dispatch_get_main_queue () is a deferred operation in the main queue, if replaced by
The global queue will perform operations on other threads! ~ Cow B! ~

-Disposable Code
Dispatch_once ^{
}
Note: This code is guaranteed to execute only once during the entire program run! ~

-Queue group (magical!) ~
Scenario: When two time-consuming operations are complete, the UI is displayed, and after two pictures are downloaded, the merge appears on the UI

Create a queue group
dispatch_group_t group = Dispatch_group_creat ();

Open Download Task 1
Dispatch_group_async (group,global_queue,^{
or the Operation 1
})];

Open Download Task 2
Dispatch_group_async (group,global_queue,^{
or the Operation 2
})];

Wait until the tasks in the Gruop are complete and perform other operations
Dispatch_group_notify (group,main_queue^{

In the main thread, the display UI details are processed.
})];

iOS Multithreading summary-----GCD 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.