IOS intermediate-multithreading and ios Multithreading

Source: Internet
Author: User

IOS intermediate-multithreading and ios Multithreading

GCD is a C-language framework. You do not need to manually manage the memory. It is a job-oriented, non-thread-oriented, and does not need to manage the life cycle GCD tasks of the thread/queue to execute function tasks: all Block tasks are closed in the Block. -- Thread execution queue: stores the queue in the GCD task FIFO (FIFO principle): Serial queue: to execute tasks in sequence // create a serial queue dispatch_queue_t serialQueue = dispatch_queue_create ("serial", DISPATCH_QUEUE_SERIAL); concurrent queue: To execute tasks concurrently (simultaneously) // create a concurrent queue dispatch_queue_t concurrentQueue = dispatch_queue_create ("concurrent", DISPATCH_QUEUE_CONCURRENT). Two Special queue main columns: The tasks stored in the main queue column are finally handed over to the main thread for execution. // You do not need to create dispatch_queue_t mainQueue = dispatch_get_main_queue () to obtain the main queue. Global concurrent queue: there is no difference between concurrent queues. It is usually used for development. (You do not need to manage how many threads the program needs to open. The thread is automatically enabled based on the current device. // obtain the global concurrent queue. You do not need to create the thread priority before the long identifier ios8. The thread priority is DISPATCH_QUEUE_PRIORITY_DEFAULT ios8, and the previous version is compatible with 0. // <# unsigned long flags #> the reserved API has not passed 0 dispatch_queue_t globalQueue = dispatch_get_global_queue (0, 0); execution function: Put tasks in the queue into the thread to execute the synchronous execution function: not capable of enabling new threads. The task is executed in the current thread. // Synchronize dispatch_sync (concurrentQueue, ^ {NSLog (@ "Download Image 2") ;}); synchronous functions + concurrent queues and + Serial queues implement the same asynchronous execution functions: able to enable new threads. It is not certain whether to enable the thread. Queue-related // execution function // asynchronous execution // dispatch_queue_t queue // ^ (void) block task dispatch_async (serialQueue, ^ {NSLog (@ "Download image") ;}); Role of GCD: 1> Add a task to the queue 2> use the execution function to execute the Task 2 execution functions * 3 queue synchronization functions example
1 // synchronous Function + Serial queue 2 // The New thread is not enabled and the task is executed in the current thread. 3 // if the current thread is a subthread, it is to execute 4 // The task is executed in order 5-(void) test1 6 {7 dispatch_queue_t serialQueue = dispatch_queue_create (nil, DISPATCH_QUEUE_SERIAL); 8 // synchronous function 9 dispatch_sync (serialQueue, ^ {10 // task 11 NSLog (@ "Download image 1% @", [NSThread currentThread]); 12}); 13 // synchronization function 14 dispatch_sync (serialQueue, ^ {15 // task 16 NSLog (@ "Download image 2% @", [NSThread currentThread]); 17}); 18 // synchronous function 19 dispatch_sync (serialQueue, ^ {20 // task 21 NSLog (@ "Download image 3% @", [NSThread currentThread]); 22 }); 23} 24 25 // synchronous Function + global concurrent queue 26 // The New thread is not enabled, and the current thread executes 27 // if the current thread is a subthread, is to execute 28 // The task in the subthread executes 29-(void) test230 {31 dispatch_queue_t queue = dispatch_get_global_queue (0, 0); 32 dispatch_sync (queue, ^ {33 NSLog (@ "Download image 1% @", [NSThread currentThread]); 34}); 35 dispatch_sync (queue, ^ {36 NSLog (@ "Download image 2% @", [NSThread currentThread]); 37}); 38 dispatch_sync (queue, ^ {39 NSLog (@ "Download image 3% @", [NSThread currentThread]); 40 }); 41} 42 // the synchronous Function + Serial queue and + concurrent queue have the same execution effect. 43 // synchronization function + 44 in the main queue column // all tasks in the main queue column are handed over to the main thread for execution. 45 // tasks in the main thread and in the main queue column wait for each other and cannot be executed. 46-(void) test347 {48 dispatch_sync (dispatch_get_main_queue (), ^ {49 NSLog (@ "downloading image 1% @", [NSThread currentThread]); 50}); 51 dispatch_sync (dispatch_get_main_queue (), ^ {52 NSLog (@ "downloading image 2% @", [NSThread currentThread]); 53}); 54 dispatch_sync (dispatch_get_main_queue (), ^ {55 NSLog (@ "Download image 3% @", [NSThread currentThread]); 56}); 57 NSLog (@ "test3End % @", [NSThread currentThread]); 58 59}

 

Asynchronous function example
1 // asynchronous Function + main column 2 // new thread not enabled 3 // task execution in order 4 // master thread 5 dispatch_async (dispatch_get_main_queue (), ^ {6 7 NSLog (@ "Download image 1% @", [NSThread currentThread]); 8}); 9 // asynchronous Function + main column 10 dispatch_async (dispatch_get_main_queue (), ^ {11 NSLog (@ "Download image 2% @", [NSThread currentThread]); 12}); 13 // asynchronous Function + main column 14 dispatch_async (dispatch_get_main_queue (), ^ {15 NSLog (@ "Download image 3% @", [NSThread currentThread]); 16}); 17 18 // asynchronous Function + Serial queue 19 // start a new thread. 20 // a queue corresponds to a thread. tasks in each thread are executed in sequence-serial execution. 21 // if multiple serial queues are created, multiple threads are enabled. 22-(void) test123 {24 dispatch_queue_t queue = dispatch_queue_create (nil, queue); 25 bytes queue1 = dispatch_queue_create (nil, queue); 26 dispatch_async (queue, ^ {27 NSLog (@ "downloading image 1% @", [NSThread currentThread]); 28}); 29 dispatch_async (queue1, ^ {30 NSLog (@ "download images 2% @", [NSThread currentThread]); 31}); 32 33 dispatch_async (queue1, ^ {34 NSLog (@ "downloading image 3% @", [NSThread currentThread]); 35}); 36 37 dispatch_async (queue, ^ {38 NSLog (@ "Download image 4% @", [NSThread currentThread]); 39 }); 40} 41 42 // asynchronous Function + global concurrent queue 43 // start a thread and execute task 44 in the Child thread // enable multiple threads based on the number of tasks. 45 // GCD automatically enables a certain number of threads. 46-(void) test247 {48 dispatch_queue_t queue = dispatch_get_global_queue (0, 0); 49 // asynchronous Function + global concurrent queue 50 dispatch_async (queue, ^ {51 NSLog (@ "Download image 1% @", [NSThread currentThread]); 52}); 53 // asynchronous Function + global concurrent queue 54 dispatch_async (queue, ^ {55 NSLog (@ "Download image 2% @", [NSThread currentThread]); 56}); 57} 58 59 // One-time code, executed only once, ensure thread security. 60 // After the thread executes the task for the first time, other threads will not enter. 61 static dispatch_once_t onceToken; 62 dispatch_once (& onceToken, ^ {63 NSLog (@ "% @", [NSThread currentThread]); 64 NSLog (@ "one-time code, run only once % @ ", string); 65}); 66 67 // use 68-(void) groupTest69 {70 _ block UIImage * image1, * image2; 71 // create a queue group 72 dispatch_group_t group = dispatch_group_create (); 73 // put the task we need to execute in the queue 74 // Asynchronous Method 75 dispatch_group_async (group, dispatch_get_global_queue (0, 0), ^ {76 // task 177 NSLog (@ "renwu 1"); 78 image1 = [self downloadImageWithUrlString: @ "http://e.hiphotos.baidu.com/zhidao/pic/item/203fb80e7bec54e722c5454ebb389b504fc26ab0.jpg"]; 79 }); 80 // Add a task 81 dispatch_group_async (group, dispatch_get_global_queue (0, 0), ^ {82 // task 283 NSLog (@ "renwu 2") to the queue group "); 84 image2 = [self downloadImageWithUrlString: @ "http://www.cnwan.com.cn/uploads/allimg/2011-07/01002414-1-10j14.jpg"]; 85}); when all tasks in the queue group are completed, a notification is sent, run the following method 87 dispatch_group_notify (group, dispatch_get_main_queue (), ^ {88 // operation 89 NSLog (@ "renwu over") after the tasks in the queue group are completed "); 90 // merge Image 91 self. imageView. image = [self BingImageWith: image1: image2]; 92}); 93}

 

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.