GCD
Full name is Grand Central Dispatch
Characteristics:
- Multi-core technology for automatic CPU utilization
- Automatically manage the life cycle of threads
Use steps
- customizing tasks
- Add a task to a queue
Features of various types of queues
Two modes of execution for synchronous and asynchronous
/** * 同步方式执行任务,不管是什么队列,都不会再开一个线程 */ dispatch_sync(<#dispatch_queue_t queue#>, ^{ <#code#> }) /** * 异步方式执行任务,除了主队列都会开启一个新线程 */ dispatch_async(<#dispatch_queue_t queue#>, ^{ <#code#> })
Create a queue global concurrent queue
//获得全局的并发队列,第一个参数是优先级,一般都是0,第二个参数是预留的,也为0 dispatch_get_global_queue(<#long identifier#>, <#unsigned long flags#>)
Use
dispatch_queue_t queue = dispatch_get_global_queue(00); /** * 同步方式执行任务 */ dispatch_sync(queue, ^{ NSLog(@"%@", NSThread.currentThread);//{number = 1, name = main} }); /** * 异步方式执行任务 */ dispatch_async(queue, ^{ NSLog(@"%@", NSThread.currentThread);//{number = 2, name = (null)} });
Serial queue
//第一个参数是队列的名字,第二个参数为nildispatch_queue_create(<#constchar *label#>, <#dispatch_queue_attr_t attr#>)
dispatch_queue_t queue = dispatch_queue_create("ttf", nil); /** * 同步方式执行任务 */ dispatch_sync(queue, ^{ NSLog(@"%@", NSThread.currentThread); }); /** * 异步方式执行任务 */ dispatch_async(queue, ^{ NSLog(@"%@", NSThread.currentThread); });
Using the primary queue
The primary queue cannot be used in conjunction with synchronous execution, or it will deadlock
To use the asynchronous execution mode
dispatch_queue_t queue = dispatch_get_main_queue();
dispatch_queue_t queue = dispatch_get_main_queue(); /** * 同步方式执行任务 */// dispatch_sync(queue, ^{// NSLog(@"%@", NSThread.currentThread);// }); /** * 异步方式执行任务 */ dispatch_async(queue, ^{ NSLog(@"%@", NSThread.currentThread); });
Communication between threads
dispatch_async(dispatch_get_global_queue(00), ^{ // 执行耗时的异步操作... dispatch_async(dispatch_get_main_queue(), ^{ // 回到主线程,执行UI刷新操作 });});
Download Images asynchronously
Uiimageview*imageview = [[UiimageviewALLOC] init]; ImageView. Frame= CGRectMake ( -, -, -, -); ImageView. BackgroundColor= [UicolorRedcolor]; [ Self. ViewAddsubview:imageview];dispatch_queue_tQueue = Dispatch_get_global_queue (0,0);Dispatch_async(Queue, ^{Nsurl*url = [Nsurlurlwithstring:@"Http://img6.cache.netease.com/cnews/2015/5/13/20150513153022b6a55.jpg"]; NSData *data = [NSData Datawithcontentsofurl:url];//This guild is time consuming UIImage*image = [UIImageImagewithdata:data];Dispatch_async(Dispatch_get_main_queue (), ^{ImageView. Image= Image; }); });
Disposable code
Code the entire program is executed only once
staticdispatch_once_t onceToken;dispatch_once(&onceToken, ^{ // 只执行1次的代码(这里面默认是线程安全的)});
Queue Group
Can be a few lines preempted into the queue group inside, waiting for the thread in this group to complete the execution, and then do other things
dispatch_group_t group = 0), ^{ // 执行10), ^{ // 执行1个耗时的异步操作});dispatch_group_notify(group, dispatch_get_main_queue(), ^{ // 等前面的异步操作都执行完毕后,回到主线程...});
"iOS Development-multithreading" using GCD to create multithreading (iOS common technology)