Step 4 GCD Queue Walkthrough

Source: Internet
Author: User

Serial Queue

Characteristics

    • Task execution in a sequential dispatch queue in FIFO mode
    • Regardless of whether the execution task function specified in the queue is synchronous or asynchronous, it waits for the previous task to complete before dispatching the subsequent task

Queue creation

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_serial);

dispatch_queue_t queue = dispatch_queue_create ("QueueName", NULL);

Serial Queue Walkthroughs

1 Serial Queue synchronization execution

/**

Question: Is it a thread? Are they executed sequentially? Where's come here?

*/

-(void) GcdDemo1 {

1. Queue

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_serial);

2. Perform tasks

for (int i = 0; i < ++i) {

NSLog (@ "---%d", i);

Dispatch_sync (q, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

}

NSLog (@ "come here");

}

    • Serial Queue Asynchronous execution

/**

Question: Is it a thread? Are they executed sequentially? Where's come here?

*/

-(void) GcdDemo2 {

1. Queue

dispatch_queue_t q = dispatch_queue_create ("foo", NULL);

2. Perform tasks

for (int i = 0; i < ++i) {

NSLog (@ "---%@%d", [Nsthread CurrentThread], i);

Dispatch_async (q, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

}

NSLog (@ "come here");

}

Concurrent Queues

Characteristics

    • Task execution in the queue for concurrent scheduling in FIFO mode
    • If the currently scheduled task is performed synchronously, it waits for the task to complete before dispatching subsequent tasks
    • If the currently scheduled task is executed asynchronously, and the underlying thread pool has available threads resources , new threads are dispatched to perform the subsequent tasks

Queue creation

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_concurrent);

Concurrent Queue Walkthroughs

    • Concurrent queue Asynchronous execution

/**

Question: Is it a thread? Are they executed sequentially? Where's come here?

*/

-(void) GcdDemo3 {

1. Queue

dispatch_queue_t q = dispatch_queue_create ("foo", dispatch_queue_concurrent);

2. Perform tasks

for (int i = 0; i < ++i) {

Dispatch_async (q, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

}

NSLog (@ "come here");

}

    • Concurrent Queue Synchronization Execution

/**

Question: Is it a thread? Are they executed sequentially? Where's come here?

*/

-(void) GcdDemo4 {

1. Queue

dispatch_queue_t q = dispatch_queue_create ("foo", dispatch_queue_concurrent);

2. Perform tasks

for (int i = 0; i < ++i) {

Dispatch_sync (q, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

NSLog (@ "--->%i", i);

}

NSLog (@ "come here");

}

Home Row

Characteristics

    • A queue dedicated to scheduling tasks on the main thread
    • does not open threads
    • The tasks in the queue are scheduled to execute on the main thread when the main thread is idle in FIFO
    • If the current main thread is executing a task, it will not be dispatched regardless of what task is currently added to the main queue

Queue fetching

    • The primary queue is the one that is responsible for scheduling tasks on the main thread
    • is created along with the program startup
    • The primary queue only needs to be fetched without creating

dispatch_queue_t queue = Dispatch_get_main_queue ();

Master Queue Walkthrough

    • Primary queue, asynchronous execution

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event {

[Self gcdDemo1];

[Nsthread sleepfortimeinterval:1];

NSLog (@ "over");

}

-(void) GcdDemo1 {

dispatch_queue_t queue = Dispatch_get_main_queue ();

for (int i = 0; i < ++i) {

Dispatch_async (Queue, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

NSLog (@ "--->%d", i);

}

NSLog (@ "come here");

}

Tasks in the queue are scheduled to execute on the main thread when the main thread is idle

    • Primary queue, synchronous execution

MARK: Master Queue, synchronization task

-(void) GcdDemo6 {

1. Queue

dispatch_queue_t q = Dispatch_get_main_queue ();

NSLOG (@ "!!!");

2. Synchronous

Dispatch_sync (q, ^{

NSLog (@ "%@", [Nsthread CurrentThread]);

});

NSLog (@ "come here");

}

The main queue and the main thread are waiting for each other to cause a deadlock

The role of synchronization tasks

A synchronization task that allows other asynchronous tasks to be performed, depending on one of the synchronization tasks

For example: After the user logs in, and then asynchronously download the file!

-(void) GcdDemo1 {

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_concurrent);

Dispatch_sync (Queue, ^{

NSLog (@ "Login%@", [Nsthread CurrentThread]);

});

Dispatch_async (Queue, ^{

NSLog (@ "Download A%@", [Nsthread CurrentThread]);

});

Dispatch_async (Queue, ^{

NSLog (@ "Download B%@", [Nsthread CurrentThread]);

});

}

    • Code transformation, so that logins are also executed asynchronously

-(void) GcdDemo2 {

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_concurrent);

void (^task) () = ^{

Dispatch_sync (Queue, ^{

NSLog (@ "Login%@", [Nsthread CurrentThread]);

});

Dispatch_async (Queue, ^{

NSLog (@ "Download A%@", [Nsthread CurrentThread]);

});

Dispatch_async (Queue, ^{

NSLog (@ "Download B%@", [Nsthread CurrentThread]);

});

};

Dispatch_async (queue, Task);

}

1 home row scheduling synchronous queue deadlock

-(void) GcdDemo3 {

dispatch_queue_t queue = dispatch_queue_create ("QueueName", dispatch_queue_concurrent);

void (^task) () = ^ {

Dispatch_sync (Dispatch_get_main_queue (), ^{

NSLog (@ "dead?") ");

});

};

Dispatch_async (queue, Task);

}

The queue's tasks are scheduled to execute on the main thread when the main thread is idle.

Global Queue
    • The system is provided for the convenience of programmer development, and its performance is consistent with the concurrent queue

Global Queue & differences in concurrent queues

1 Global queues

? No Name

? Neither MRC & ARC need to consider releasing

? In daily development, it is recommended to use "Global queue"

2 Concurrent queues

? Has a name that works like the name property of Nsthread

? If you need to use Dispatch_release (q) during MRC development; Release the appropriate object

? Dispatch_barrier must use a custom concurrent queue

? When developing a third-party framework, it is recommended to use concurrent queues

Global Queue Asynchronous Task

/**

Question: Is it a thread? Are they executed sequentially? Where's come here?

*/

-(void) GcdDemo8 {

1. Queue

dispatch_queue_t q = dispatch_get_global_queue (0, 0);

2. Perform tasks

for (int i = 0; i < ++i) {

Dispatch_async (q, ^{

NSLog (@ "%@-%d", [Nsthread CurrentThread], i);

});

}

NSLog (@ "come here");

}

The same effect as the concurrent queue

Parameters

    • Quality of service (Queue priority for task scheduling) before/ios 7.0 is the priority

IOS 8.0 (new, temporarily unavailable, end of year)

Qos_class_user_interactive 0x21, user interaction (hoping for the fastest completion-not with too time-consuming operations)

Qos_class_user_initiated 0x19, user expectations (hope fast, not too time consuming)

Qos_class_default 0x15, default (used for the underlying reset queue, not for programmers)

Qos_class_utility 0x11, utility (designed to handle time-consuming operations!) )

Qos_class_background 0x09, backstage

Qos_class_unspecified 0x00, not specified, can be adapted with iOS 7.0

IOS 7.0

Dispatch_queue_priority_high 2 High-priority

Dispatch_queue_priority_default 0 Default Priority

Dispatch_queue_priority_low (-2) low-priority

Dispatch_queue_priority_background Int16_min Background Priority

    • Reserved for future use, should always pass in 0

Conclusion: If you are adapting to IOS 7.0 & 8.0, use the following code: Dispatch_get_global_queue (0, 0);

Step 4 GCD Queue Walkthrough

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.