Asynchronous tasks + parallel queues and asynchronous tasks + Serial queues (main queue)

Source: Internet
Author: User

Asynchronous tasks + parallel queues and asynchronous tasks + Serial queues (main queue)
Asynchronous task + parallel queue

Place asynchronous tasks in parallel queues for execution. asynchronous tasks are executed in different threads.

/* Asynchronous execution + parallel queue */-(IBAction) clickBasic1 :( UIButton *) sender {// global parallel queue dispatch_queue_t queue = dispatch_get_global_queue (queue, 0 ); // asynchronous execution of dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@ "task1: % d", I );} NSLog (@ "task1 ---- % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@ "task2: % d", I);} NSLog (@ "task2 ---- % @", [NSThread currentThread]);}); dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@ "task3: % d", I );} NSLog (@ "task3 ---- % @", [NSThread currentThread]) ;});}

The running result is as follows. When asynchronous tasks and parallel queues are combined, each task is executed simultaneously in different threads.

Asynchronous task + Serial Queue (main queue)

When an asynchronous task is executed in a serial queue, the task is only executed in a new thread in order.

/* Asynchronous serial queue */-(IBAction) clickBasic3 :( id) sender {// create a serial queue dispatch_queue_t queue = dispatch_queue_create ("com. hcios ", NULL); // asynchronous execution of dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@" task1: % d ", I) ;}nslog (@" task1 ---- % @ ", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@ "task2: % d", I);} NSLog (@ "task2 ---- % @", [NSThread currentThread]) ;}); dispatch_async (queue, ^ {for (int I = 0; I <2; I ++) {NSLog (@ "task3: % d ", I);} NSLog (@" task3 ---- % @ ", [NSThread currentThread]);}

The running result is as follows. It can be seen that all tasks are executed in one thread, and after one is completed, the next task is executed.

 

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.