Introduction:
Grand Central Dispatch (GCD) is a technology developed by Apple to optimize applications that support multi-core processors and other systems of Symmetric Multi-processing systems. This is based on the Thread Pool Mode for concurrent task execution. It is released on Mac OS X 10.6 For the first time, and is also available for iOS 4 and later versions.
Design:
GCD works as follows: allows programs to queue specific tasks in parallel and schedule them to execute tasks on any available processor core based on available processing resources.
A task can be a function or a block. The underlying layer of GCD is still implemented using threads, but this allows programmers to ignore implementation details.
The FIFO queue in GCD is called dispatch queue, which ensures that advanced tasks are executed first.
Dispatch queue is divided into the following three types:
Serial
It is also called private dispatch queues and only executes one task at the same time. Serial queue is usually used to synchronously access specific resources or data. When you create multiple Serial queue, although they are executed simultaneously, the Serial queue and Serial queue are executed concurrently.
Concurrent
It is also called global dispatch queue. It can execute multiple tasks concurrently, but the execution order is random.
Main dispatch queue
It is a globally available serial queue that executes tasks on the main thread of the application.
Let's see how to use dispatch queue.
1. Common Methods: dispatch_async
To prevent the interface from getting stuck when processing time-consuming operations, such as reading network data, I/O, and database reads and writes, We will process these operations in another thread and then notify the main thread to update the interface.
Using GCD to implement this process is simpler than the NSThread NSOperation method described earlier. The code framework structure is as follows:
Dispatch_async (dispatch_get_global_queue (queue, 0), ^ {// time-consuming operations dispatch_async (dispatch_get_main_queue (), ^ {// update interface });});
If this is not clear, we will use the download images from the two blogs as an example. The Code is as follows:
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ NSURL * url = [NSURL URLWithString:@"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg"]; NSData * data = [[NSData alloc]initWithContentsOfURL:url]; UIImage *image = [[UIImage alloc]initWithData:data]; if (data != nil) { dispatch_async(dispatch_get_main_queue(), ^{ self.imageView.image = image; }); } });
Run display:
Is the code much simpler than NSThread NSOperation, and GCD automatically allocates resources and optimizes programs on multi-core processors based on tasks.
The system provides three concurrent dispatch queues for each application. These three concurrent scheduling queues are global, and they only have different priorities. Because it is global, we do not need to create it. We only need to use the dispath_get_global_queue function to get the queue, as shown below:
dispatch_queue_t globalQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
The system has a serial queue main_queue by default.
dispatch_queue_t mainQ = dispatch_get_main_queue();
Although dispatch queue is a reference counting object, both of the above are global queues without retain or release.
2. Use dispatch_group_async
Dispatch_group_async can be used to monitor whether a group of tasks are completed, and other operations are notified after completion. This method is very useful. For example, if you execute three download tasks, you will be notified after all the three download tasks are completed. The following is an example code:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_group_t group = dispatch_group_create(); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"group1"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"group2"); }); dispatch_group_async(group, queue, ^{ [NSThread sleepForTimeInterval:3]; NSLog(@"group3"); }); dispatch_group_notify(group, dispatch_get_main_queue(), ^{ NSLog(@"updateUi"); }); dispatch_release(group);
Dispatch_group_async is an asynchronous method. The print result is displayed after running:
16:04:16. 737 gcdTest [43328: 11303] group1
16:04:17. 738 gcdTest [43328: 12a1b] group2
16:04:18. 738 gcdTest [43328: 13003] group3
16:04:18. 739 gcdTest [43328: f803] updateUi
Print one task per second. When the third task is executed, the upadteUi is printed.
3. Use dispatch_barrier_async
Dispatch_barrier_async is executed only after the previous task is executed, and the subsequent task will be executed only after it is executed.
The sample code is as follows:
dispatch_queue_t queue = dispatch_queue_create("gcdtest.rongfzh.yc", DISPATCH_QUEUE_CONCURRENT); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:2]; NSLog(@"dispatch_async1"); }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:4]; NSLog(@"dispatch_async2"); }); dispatch_barrier_async(queue, ^{ NSLog(@"dispatch_barrier_async"); [NSThread sleepForTimeInterval:4]; }); dispatch_async(queue, ^{ [NSThread sleepForTimeInterval:1]; NSLog(@"dispatch_async3"); });
Print result:
16:20:33. 967 gcdTest [45547: 11203] dispatch_async1
16:20:35. 967 gcdTest [45547: 11303] dispatch_async2
16:20:35. 967 gcdTest [45547: 11303] dispatch_barrier_async
16:20:40. 970 gcdTest [45547: 11303] dispatch_async3
Pay attention to the execution time. You can see the execution sequence as described above.
4. dispatch_apply
Execute a code snippet N times.
Dispatch_apply (5, globalQ, ^ (size_t index ){
// Execute 5 times
});
The example code used in this article: http://download.csdn.net/detail/totogo2010/4596471
GCD has many other usage options. For more information, see the official documentation.
Reference documents also include: http://en.wikipedia.org/wiki/Grand_Central_Dispatch
The first two multi-thread blog posts: NSThread for iOS multi-thread programming
NSOperation and NSOperationQueue for iOS multi-thread programming
Copyright Disclaimer: This article will be published at http://blog.csdn.net/totogo2010/, and you will be welcomed to enjoy the transfer and sharing. Please respect the work of the author. Keep this note and the author's blog link when reprinting. Thank you!