//
Viewcontroller.m
Ui-no.26-gcd
//
Created by Bruce on 15/8/27.
Copyright (c) 2015 Bruce. All rights reserved.
//
/*
Grand Central Dispatch (GCD) is a solution to the multi-core programming that Apple advocates
The GCD layer is also implemented using threads, which instructs Apple to encapsulate the method of operation, so that the programmer does not have to focus on the details of the implementation, only the implementation of the focus function
GCD parameter queue: Queue block: Task
GCD There are two ways to perform a task 1, synchronous Dispatch_sync (avoid using synchronization in the main queue to perform the main thread task easily deadlock) 2, asynchronous Dispatch_async
The difference between synchronous and asynchronous:
Synchronization: Executing in the current thread
Async: Executing in another thread
There are two types of < queue > methods that GCD perform multiple tasks:
1. Serial queue: Let the task execute one after another (once a task is completed, the next task is executed)
2. Concurrent queue: Multiple tasks can be executed concurrently (simultaneous execution of multiple threads) concurrency function is only valid under asynchronous (Dispatch_async) functions
Serial queues are also divided into two types:
1, the host row Dispatch_get_main_queue () The home row is a special serial queue of GCD, and the tasks placed in the master queue are placed in the main thread to execute
2. Create a queue dispatch_queue_create
Concurrent queues: GCD provides a global concurrency queue by default for the entire application, without the need to manually create
Dispatch_get_global_queue
The first parameter inside is to set his priority.
#define DISPATCH_QUEUE_PRIORITY_HIGH 2//High
#define DISPATCH_QUEUE_PRIORITY_DEFAULT 0//default (Medium)
#define Dispatch_queue_priority_low (-2)//Low
#define Dispatch_queue_priority_background int16_min//Backstage
GCD Deferred submission Tasks
When: the time specified
Queue: The specified queues
Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (5 * nsec_per_sec)), Dispatch_get_main_queue (), ^{
NSLog (@ "deferred submission task");
});
GCD to Group tasks
Sometimes, we may perform a series of tasks. Because of the dependency between them. For example, there are 3 tasks: A, B, C; We must execute task A to execute task B and finally execute task C. In this case, we can use the GCD grouping mechanism to perform multiple tasks in a predetermined order.
Creating a group
dispatch_group_t Creatgroup = Dispatch_group_create ();
Create a queue
dispatch_queue_t queue = Dispatch_get_main_queue ();
Dispatch_group_async (Creatgroup, queue, ^{
NSLog (@ "%@", [Nsthread CurrentThread]);
});
Dispatch_group_async (Creatgroup, queue, ^{
NSLog (@ "%@", [Nsthread CurrentThread]);
});
Dispatch_group_async (Creatgroup, queue, ^{
NSLog (@ "%@", [Nsthread CurrentThread]);
});
Dispatch_group_notify (Creatgroup, queue, ^{
NSLog (@ "Success");
});
*/
#import "ViewController.h"
@interface Viewcontroller () <UITableViewDelegate,UITableViewDataSource>
{
UITableView *mytableview;
UIImage *showimage;
Nsarray *urllist;
Nsmutabledictionary *imagedic;
}
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Imagedic = [Nsmutabledictionary dictionary];
Serial queue:
Get the Home row
dispatch_queue_t mainqueue = Dispatch_get_main_queue ();
Create a queue
dispatch_queue_t createqueue = dispatch_queue_create ("Create", NULL);
NSLog (@ "....");
Synchronous for serial queuing: The main thread waits for the home queue to schedule the synchronization task, and the home column discovers that there are still tasks on the main thread that do not finish, so the synchronization task is not added to the main thread, resulting in a deadlock caused by waiting for each other.
Dispatch_sync (Mainqueue, ^{
NSLog (@ "1111");
// });
Viewdidload Insert synchronous operation to Dispatch_get_main_queue () when performing to sync in the main thread
Sync will wait until the next block executes to return, Sync again dispatch_get_main_queue () queue,
It is a serial queue, Sync is added after, the previous one is the main thread,
So sync wants to execute the block must wait for the main thread to finish, the main thread waits for sync to return, to perform the subsequent content.
Causes a deadlock, sync waits for Mainthread to finish, Mianthread waits for the sync function to return.
Asynchronous
Dispatch_async (Mainqueue, ^{
NSLog (@ "1111");
// });
Dispatch_sync (Createqueue, ^{
NSLog (@ "1111");
// });
Dispatch_sync (Createqueue, ^{
NSLog (@ "2222");
// });
Dispatch_sync (Createqueue, ^{
NSLog (@ "3333");
// });
Dispatch_async (Createqueue, ^{
NSLog (@ "1111");
// });
Dispatch_async (Createqueue, ^{
NSLog (@ "2222");
// });
Dispatch_async (Createqueue, ^{
NSLog (@ "3333");
// });
Concurrent queues
dispatch_queue_t queue = Dispatch_get_global_queue (dispatch_queue_priority_low, 0);
dispatch_queue_t queue1 = dispatch_get_global_queue (dispatch_queue_priority_default, 0);
dispatch_queue_t queue2 = dispatch_get_global_queue (dispatch_queue_priority_high, 0);
Concurrent queues are valid only in asynchronous situations
Dispatch_async (Queue, ^{
NSLog (@ "111");
// });
Dispatch_async (Queue1, ^{
NSLog (@ "1111");
// });
Dispatch_async (Queue2, ^{
NSLog (@ "11111");
// });
dispatch_group_t group = Dispatch_group_create ();
Dispatch_sync (Queue, ^{
NSLog (@ "1 ...");
});
Dispatch_sync (Queue, ^{
NSLog (@ "2 ...");
});
Dispatch_sync (Queue, ^{
NSLog (@ "3 ...");
});
Dispatch_group_notify (group, queue, ^{
NSLog (@ "OK ...");
});
dispatch_group_t group1 = Dispatch_group_create ();
Dispatch_sync (Queue1, ^{
NSLog (@ "1...1");
});
Dispatch_sync (Queue1, ^{
NSLog (@ "2...1");
});
Dispatch_sync (Queue1, ^{
NSLog (@ "3...1");
});
Dispatch_group_notify (group1, queue1, ^{
NSLog (@ "OK ... 1 ");
});
urllist = @[@ "http://image.tianjimedia.com/uploadImages/2012/273/M6J97CZGYA4Z_NatGeo01_250.jpg", @ " Http://image.tianjimedia.com/uploadImages/2012/273/J3ME8ZNAG315_NatGeo02_250.jpg ", @"/http Image.tianjimedia.com/uploadimages/2012/273/el9230ap5196_natgeo03_250.jpg ", @" http://image.tianjimedia.com/ Uploadimages/2012/273/syj43sg47pc8_natgeo04_250.jpg ", @" http://image.tianjimedia.com/uploadImages/2012/273/ 605x52620g0m_natgeo05_250.jpg "@" http://image.tianjimedia.com/uploadImages/2012/273/7H5RQ1ELP8MZ_NatGeo06_250. JPG "@" http://image.tianjimedia.com/uploadImages/2012/273/Z2W429E0203R_NatGeo07_250.jpg "@"/http Image.tianjimedia.com/uploadimages/2012/273/bg011w9lwl77_natgeo08_250.jpg ", @" http://image.tianjimedia.com/ Uploadimages/2012/273/936fm8nn22j2_natgeo09_250.jpg ", @" http://image.tianjimedia.com/uploadImages/2012/273/ H79633ppefzw_natgeo10_250.jpg "@" http://image.tianjimedia.com/uploadImages/2012/273/54Z01YZ78050_NatGeo11_250. JPG "@" http://image.tianjimedia.com/uploadImAges/2012/273/61v3658ua4iy_natgeo12_250.jpg ", @" Http://image.tianjimedia.com/uploadImages/2012/273/H3HL7YILNGKB _natgeo13_250.jpg ", @" http://image.tianjimedia.com/uploadImages/2012/273/1V6797311ZA5_NatGeo14_250.jpg ", @" http ://image.tianjimedia.com/uploadimages/2012/273/93l81ikn156r_natgeo15_250.jpg ", @" http://image.tianjimedia.com/ Uploadimages/2012/273/v93e1egu2g0z_natgeo16_250.jpg ", @" http://image.tianjimedia.com/uploadImages/2012/273/ Qc205cd96iwz_natgeo17_250.jpg "];
Mytableview = [[UITableView alloc]initwithframe:self.view.frame];
Mytableview.delegate = self;
Mytableview.datasource = self;
[Self.view Addsubview:mytableview];
}
-(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath
{
static NSString *cellidentifier = @ "Cell";
UITableViewCell *cell = [TableView dequeuereusablecellwithidentifier:cellidentifier];
if (!cell) {
cell = [[UITableViewCell alloc]initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier];
}
__block UIImage *images = [Imagedic objectforkey:@ (Indexpath.row)];
if (!images) {
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
Nsurl *url = [Nsurl Urlwithstring:urllist[indexpath.row]];
Nsurlrequest * request = [nsurlrequest Requestwithurl:url];
NSData * data = [nsurlconnection sendsynchronousrequest:request returningresponse:nil Error:nil];
if (data) {
Images = [UIImage imagewithdata:data];
[Imagedic setobject:images forkey:@ (Indexpath.row)];
}
After the download is complete, go to the main Thread Update table cell
Dispatch_async (Dispatch_get_main_queue (), ^{
[TableView Reloadrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationfade];
});
});
}
Cell.imageView.image = images;
return cell;
}
-(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) Section
{
return urllist.count;
}
-(CGFloat) TableView: (UITableView *) TableView Heightforrowatindexpath: (Nsindexpath *) Indexpath
{
return 100;
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any resources the can be recreated.
}
@end
GCD (2)