GCD Base (Queue)

Source: Internet
Author: User

Taking notes... -------

1. Serial queue-One after another execution
Create a queue
dispatch_queue_t q = dispatch_queue_create ("GCD1", dispatch_queue_serial);
for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}

for (int i = 0; i < ++i) {
asynchronous tasks, which are executed concurrently, but if in the serial queue, they are still sequentially executed
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}

2. Parallel queue
Features: No formation, execution sequence programmer can not control!
Scenario: Concurrent execution tasks, no sequencing relationship
Parallel queues are prone to errors! Parallel queues do not control the number of new threads!
dispatch_queue_t q = dispatch_queue_create ("Gcd2", dispatch_queue_concurrent);

for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}



for (int i = 0; i < ++i) {
Asynchronous tasks are executed in different threads
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}

3. Global Queue (Apple provides a global queue for easy multi-threaded design for all apps to work with)
The difference between a global queue and a parallel queue
1> does not need to be created, direct get can be used
2> two queues perform the same effect
3> Global queue does not have a name, the exact queue cannot be confirmed when debugging
dispatch_queue_t Q =dispatch_get_global_queue (dispatch_queue_priority_default, 0);

for (int i = 0; i < ++i) {
Synchronous Task Order Execution
Dispatch_sync (q, ^{
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}

for (int i = 0; i < ++i) {
asynchronous tasks, which are executed concurrently, but are still executed sequentially if in the walkthrough queue
Dispatch_async (q, ^{
[Nsthread CurrentThread] can track the current thread in development
num = 1, which indicates the main thread
num = 2, which represents the 2nd child thread ...
NSLog (@ "%@%d", [Nsthread CurrentThread], i);
});
}

4. Primary (thread) queue, which ensures that operations are performed on the main thread
Each application has only one main thread
Why do I need to work on the main thread?
In iOS development, all UI updates must be done on the main thread!
dispatch_queue_t q = Dispatch_get_main_queue ();

The main thread is working, and unless the program is killed, the main thread's work will never end!
Blocked!!! So don't use sync
Dispatch_sync (q, ^{
NSLog (@ "Come here baby!");
});

Asynchronous tasks that run on the main thread while maintaining formation
for (int i = 0; i < ++i) {
Dispatch_async (q, ^{
NSLog (@ "%@-%d", [Nsthread CurrentThread], i);
});
}


GCD Base (Queue)

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.