Basic knowledge:
9:09
First, the basic concept
1. What is GCD
The full name of the Grand Central Dispath Pure C language, providing a very large and powerful function, is currently the recommended multithreaded development approach, Nsoperation is GCD-based encapsulation
2, the advantages of GCD
1. propose a solution for multi-core parallel operation
2.GCD will automatically take advantage of more CPU cores, such as dual-core, quad-core
3.GCD automatically manages the life cycle of threads (create threads, schedule tasks, destroy threads)
4. programmers just need to tell GCD what tasks they want to perform, without writing any thread management code
3. Two core concepts in GCD
1. Task: What action to perform
2. queues: For storing tasks
4, the queue can be divided into two major types
(1) Serial queue (Serial Dispatch queue): There can only be one thread, the actions added to the queue are executed sequentially, and one task is completed before the next task is executed
(2) Concurrent queues (Concurrent Dispatch queue): Can have multiple threads, which he will arrange on the available processors, while ensuring that advanced tasks are prioritized
(3) There is also a special queue that is the home row, where there is always only one thread in the master queue-the main course, which is used to perform the main thread's operation task
5, using GCD long thread, can be abstracted as two steps
1. Find the queue
2.perform tasks in a queue in a synchronous or asynchronous manner
6. Two ways to perform tasks in a queue
1, synchronization: Can only perform tasks on the current thread, does not have the ability to open new threads
2.Async: Can perform tasks in new threads with the ability to open new threads
7. There are four ways to create a thread task GCD
Second, serial synchronous serial asynchronous parallel synchronous parallel asynchronous use
#pragmaMark-----Serial Syncdispatch_queue_t Serialqueue= Dispatch_queue_create ("Serialqueue", dispatch_queue_serial); Dispatch_sync (Serialqueue,^{//NSLog (@ "%@", [Nsthread CurrentThread]); });#pragmaMark-----Serial Asyncdispatch_queue_t serialQueue1= Dispatch_queue_create ("serialQueue1", dispatch_queue_serial); Dispatch_async (SerialQueue1,^{//NSLog (@ "%@", [Nsthread CurrentThread]); });#pragmaMark----Parallel synchronizationdispatch_queue_t Concurrentqueue= Dispatch_queue_create ("Concurrentqueue", dispatch_queue_concurrent); Dispatch_sync (Concurrentqueue,^{//NSLog (@ "%@", [Nsthread CurrentThread]); });#pragmaMark----Parallel asynchronousdispatch_queue_t concurrentQueue1= Dispatch_queue_create ("concurrentQueue1", dispatch_queue_concurrent); Dispatch_async (ConcurrentQueue1,^{NSLog (@"%@", [Nsthread CurrentThread]); });
Third, the concrete instance uses the GCD to load several pictures
#defineKurl @ "http://Store.storeimages.cdn-apple.com/8748/as-images.apple.com/is/image/appleinc/aos/published/images/s/38/s38ga /rdgd/s38ga-rdgd-sel-201601?wid=848&hei=848&fmt=jpeg&qlt=80&op_sharpen=0&resmode=bicub &op_usm=0.5,0.5,0,0&iccembed=0&layer=comp&.v=1454777389943 "@interface Moreimageviewviewcontroller () {intImagetag; Uiimageview*Myimageview; dispatch_queue_t Concurentqueue; Nsoperationqueue*Operationqueues;} @end- (void) viewdidload {[Super viewdidload]; Imagetag= -; Self.view.backgroundColor=[Uicolor Greencolor]; Self.edgesforextendedlayout=Uirectedgenone; [Self controlbtn]; /*1. Create multiple views 2, find parallel queue 3, assign multiple tasks to this parallel queue 4, loads on subnet 5, back to main thread 6, update UI*///1. Create Multiple Views for(intI=0; i<3; i++) { for(intj=0; j<2; J + +) {Myimageview= [[Uiimageview alloc]initwithframe:cgrectmake (Ten+j* $, ++i* $, the, the)]; Myimageview.backgroundcolor=[Uicolor Orangecolor]; Myimageview.tag= imagetag++; [Self.view Addsubview:myimageview]; } }//2. Find the parallel queue//use the following method to not order because the following sentence is looking for the system's global parallel queue//concurentqueue = dispatch_get_global_queue (0, 0);//This method is sequential with the serial queueConcurentqueue = Dispatch_queue_create ("Concurentqueue", dispatch_queue_serial); //3. Assigning Tasks for(intindex=0; index<6; index++) {Dispatch_async (Concurentqueue,^{[Nsthread sleepfortimeinterval:1];//Load network resourcesNSData *data =[NSData Datawithcontentsofurl:[nsurl Urlwithstring:kurl]; UIImage*image =[UIImage Imagewithdata:data]; //5. Back to the main threaddispatch_queue_t Mainqueue =Dispatch_get_main_queue (); Dispatch_sync (Mainqueue,^{ //6. Refresh the UI for(intI=0; i<6; i++) {Uiimageview*iamgeview = [Self.view viewwithtag: -+index]; Iamgeview.image=image; } }); }); The following two methods are to pause and open the thread's- (void) controlbtn{Uisegmentedcontrol*segment = [[Uisegmentedcontrol alloc]initwithitems:@[@"Pause",@"Open",]]; Segment.frame= CGRectMake ( -,620, -, -); Segment.apportionssegmentwidthsbycontent=YES; [Self.view addsubview:segment]; [Segment Addtarget:self Action: @selector (clicksegment:) forcontrolevents:uicontroleventvaluechanged];}- (void) Clicksegment: (Uisegmentedcontrol *) Sender {Switch(sender.selectedsegmentindex) { Case 0:{ //Pause Queuedispatch_suspend (Concurentqueue); } Break; Case 1:{ //Recovery QueueDispatch_resume (Concurentqueue); } Break; } }
Multi-Threading GCD for iOS development