Review Knowledge Points: GCD multithreading

Source: Internet
Author: User

The foundation of GCD

1 #pragmaMark-Create a serial queue using GCD2     //First: System-provided methods for creating a serial queue3     //in real development if you need to create a serial queue, it is more customary to use this4 //dispatch_queue_t queue = Dispatch_get_main_queue ();5 6 #pragmaMark-use GCD to create a parallel queue7     //the first type: System8     //parameter 1: Priority (four, no significant difference)9     //Parameter 2: System reserved wordTen //dispatch_queue_t queue = dispatch_get_global_queue (dispatch_queue_priority_background, 0); One  A #pragmaMark-Create child threads using GCD parallel queue -     //parameter 1: queue name -     //parameter 2:block//dispatch_async (queue, ^{ the //NSLog (@ "main1 =%@", [Nsthread Mainthread]); - //NSLog (@ "current1 =%@", [Nsthread CurrentThread]); - //    }); - //dispatch_async (queue, ^{ + //NSLog (@ "main2 =%@", [Nsthread Mainthread]); - //NSLog (@ "Current2 =%@", [Nsthread CurrentThread]); + //    }); A //dispatch_async (queue, ^{ at //NSLog (@ "main3 =%@", [Nsthread Mainthread]); - //NSLog (@ "Current3 =%@", [Nsthread CurrentThread]); - //    }); - //....  Create a N -  - #pragmaMark--a few seconds to do everything. in //Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0 * nsec_per_sec)), Dispatch_get_main_queue (), ^{  - //NSLog (@ "3 seconds"); to //    }); +  - #pragmaMark-Repeat to add multiple tasks like a queue the //dispatch_queue_t queue = dispatch_queue_create (0, dispatch_queue_concurrent); *     //First parameter: Number of additions $     //second parameter: queuePanax Notoginseng     //Third parameter: Block - //dispatch_apply (+, queue, ^ (size_t index) { the //NSLog (@ "%ld", index); + //    }); A  the #pragmaMark-Group + //    //Create a group - //dispatch_group_t group = Dispatch_group_create (); $ //    //Create a queue $ //dispatch_queue_t queue = dispatch_queue_create (0, dispatch_queue_concurrent); - //    //Create Task 1: - //Dispatch_group_async (Group, queue, ^{ the //NSLog (@ "I am 1"); - //    });Wuyi //    //Create Task 2: the //Dispatch_group_async (Group, queue, ^{ - //NSLog (@ "I am 2"); Wu //    }); - //....  Create a N About  $ //    //{So this function code must be placed after all tasks} to listen for the condition that the task is known to execute - //dispatch_group_notify (Group, queue, ^{ - //NSLog (@ "I am notify"); - //    }); A  + #pragmaMark-Serial in concurrency (Wolf in sheep's clothing) the //    //queue created by serial - //    //dispatch_queue_serial Sequential Execution $ //    //dispatch_queue_concurrent unordered Execution the //dispatch_queue_t queue = dispatch_queue_create (0, dispatch_queue_concurrent); the //dispatch_async (queue, ^{ the //NSLog (@ "Test 1"); the //    }); - //dispatch_async (queue, ^{ in //NSLog (@ "Test 2"); the //    }); the //....  Create a N

Code Show:

1 #import "ViewController.h"2 3 @interfaceViewcontroller ()4 ///Create a ImageView5@property (nonatomic,strong) Uiimageview *Imgview;6 ///create a data to receive7@property (nonatomic,strong) NSData *ImageData;8 9 @endTen  One @implementationViewcontroller A  -- (void) Viewdidload { - [Super Viewdidload]; the     //additional setup after loading the view, typically from a nib. -     //Add ImageView to the view -Self.imgview =[[Uiimageview alloc] Initwithframe:[uiscreen mainscreen].bounds]; -Self.imgView.backgroundColor =[Uicolor Cyancolor]; + [Self.view AddSubview:self.imgView]; -      + [self datahandle]; A } at #pragmaMark-Parsing data -- (void) Datahandle { -__weaktypeof(self) weakself =Self ; -     //Create a child thread to request data in a child thread -Dispatch_async (Dispatch_queue_create (0, dispatch_queue_concurrent), ^{ -Nsurl *url = [Nsurl urlwithstring:@"http://pic.58pic.com/58pic/13/11/15/83b58PICXf4_1024.jpg"]; in         //Receive Data -Weakself.imagedata =[NSData Datawithcontentsofurl:url]; to         //return to main thread refresh UI +Dispatch_async (Dispatch_get_main_queue (), ^{ -             //Pass Value theWeakSelf.imgView.image =[UIImage ImageWithData:self.imageData]; *         }); $     });Panax Notoginseng      - } the  + @end

Review Knowledge Points: GCD multithreading

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.