dyfviewcontroller.m//623-07-gcd////Created by dyf on 14-6-23.//Copyright (c) 2014 ___fullusername___. All rights reserved.//#import "DYFViewController.h" @interface Dyfviewcontroller () @property (weak, nonatomic) Iboutlet Uiimageview *iconview; @end @implementation dyfviewcontroller-(void) viewdidload{[Super viewdidload];//do any addition Al Setup after loading the view, typically from a nib. /*/Async: Ability to open new threads Dispatch_async (< #dispatch_queue_t queue#>, ^{< #code #>}); Sync: No ability to open new threads Dispatch_sync (< #dispatch_queue_t queue#>, ^{< #code #>}) */}-(void) touchesb Egan: (Nsset *) touches withevent: (uievent *) event{[self testbacktomain];} -(void) Testbacktomain {//Get global concurrent queue dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_defa ULT, 0); Asynchronous queue Dispatch_async (queue, ^{NSLog (@ "-----%@", [Nsthread CurrentThread]); Download image NSString *path = @ "HTTp://image.cache.xiu8.com/live/125/125/997729.jpg "; Nsurl *url = [Nsurl Urlwithstring:path]; NSData *data = [NSData Datawithcontentsofurl:url]; UIImage *image = [UIImage imagewithdata:data]; Back to the main thread display picture Dispatch_async (Dispatch_get_main_queue (), ^{NSLog (@ "-----------%@", [Nsthread Currentthrea D]); Self.iconView.image = image; }); });} -(void) testonce {static dispatch_once_t oncetoken; Dispatch_once (&oncetoken, ^{NSLog (@ "once"); });} -(void) Testdelay {dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5.0 * nsec_per_sec)), Dispatch_get_main _queue (), ^{NSLog (@ "222"); });} /** * Use the Dispatch_async async function to add a task */-(void) Testasyncmainqueue {//1) to the main thread in the master queue. Get the home row dispatch_queue_t queue = Dispat Ch_get_main_queue (); 2. Add task to queue, perform task Dispatch_async (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Summary: Do not open new thread}/** * Use Dispatch_syncStep function to add a task to the main thread in the master queue, dead: The task cannot be executed down */-(void) Testsyncmainqueue {//1. Get the home column dispatch_queue_t queue = Dispatch_get_main_qu Eue (); 2. Add task to queue, perform task Dispatch_sync (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Summary: Do not open a new thread, all tasks in the main thread serial execution}//all function names with Create, copy, new, retain and other words, you need to use this data when you do not need to release// GCD data types do not need to do release//CF (Core Foundation) data type in ARC environment still do release-(void) testcf {cfarrayref array = Cfarraycre ATE (null, NULL, one-by-one, null); Cfrelease (array);} /** * Add a task to the serial queue with the Dispatch_sync sync function */-(void) Testsyncserialqueue {//1. Creating a serial queue dispatch_queue_t queue = Dispatch_qu Eue_create ("Cn.dongyue.queue", NULL); 2. Add task to queue, perform task Dispatch_sync (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Dispatch_sync (Queue, ^{NSLog (@ "---------2-----%@", [Nsthread CurrentThread]); }); Dispatch_sync (Queue, ^{NSLog (@ "---------3-----%@", [Nsthread CurrentThread]); }); 3.Release (MRC)//dispatch_release (queue); Summary: Do not open a new thread}/** * Add a task to the concurrent queue with the Dispatch_sync synchronization function */-(void) Testsyncglobalqueue {//1. Get global concurrency queue dispatch_queue_t qu Eue = Dispatch_get_global_queue (dispatch_queue_priority_default, 0); 2. Add task to queue, perform task Dispatch_sync (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Dispatch_sync (Queue, ^{NSLog (@ "---------2-----%@", [Nsthread CurrentThread]); }); Dispatch_sync (Queue, ^{NSLog (@ "---------3-----%@", [Nsthread CurrentThread]); }); Summary: New threads are not opened and concurrent queues lose concurrency}/** * Add tasks to the concurrent queue with the Dispatch_async synchronization function */-(void) Testasyncserialqueue {//1. Creating a serial queue DISPATC h_queue_t queue = dispatch_queue_create ("Cn.dongyue.queue", NULL); 2. Add task to queue, perform task Dispatch_async (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue, ^{NSLog (@ "---------2-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue, ^{ NSLog (@ "---------3-----%@", [Nsthread CurrentThread]); }); Summary: Open only 1 new threads, do not open new thread}/** * Add task to concurrent queue with Dispatch_async sync function */-(void) Testasyncglobalqueue {//1. Get Global concurrency Queue DISPATC h_queue_t queue = Dispatch_get_global_queue (Dispatch_queue_priority_default, 0); 2. Add task to queue, perform task Dispatch_async (queue, ^{NSLog (@ "---------1-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue, ^{NSLog (@ "---------2-----%@", [Nsthread CurrentThread]); }); Dispatch_async (Queue, ^{NSLog (@ "---------3-----%@", [Nsthread CurrentThread]); }); Summary: 3 threads are open at the same time} @end