1.GCD (Grand Centrol Dispath)
Parallel: Both macro and micro are two people holding two shovels in digging pits, digging two big pits in an hour
Concurrency: The macro is feeling they are digging holes, microscopic is they are using a shovel digging pits, an hour after they dug two small pits.
Summary: In the case of a single CPU, most of the process is concurrent, is a spade, you just a moment, I only a short interval, the user can not feel it.
Application :
GCD includes:
(1) In actual use
Dispatch_get_global_queue (0, 0) The first 0 is the priority, the second reserved field
Dispatch_async (dispatch_get_global_queue (0, 0), ^{
//Can be a data request here
nsstring* result = [self requestdata:parameter];
//Return to main thread here to refresh data
Dispatch_async (Dispatch_get_main_queue (), ^{
[Maintableview Reloaddata];
});
});
To illustrate:
Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{
Nsurl * url = [nsurl urlwithstring:@ "http://www.baidu.com"];
Nserror * ERROR;
NSString * data = [NSString stringwithcontentsofurl:url encoding:nsutf8stringencoding error:&error];
if (data! = nil) {
Dispatch_async (Dispatch_get_main_queue (), ^{
NSLog (@ "Call back, the data is:%@", data);
});
} else {
NSLog (@ "error when download:%@", error);
}
});
(2) You can create it yourself (I don't use it)
Serial queue, as the name implies, a bunch of it, it has to be executed concurrently
Create your own serial queue
dispatch_queue_t queue = dispatch_queue_create ("Com.class15.queue", dispatch_queue_serial);
Asynchronous execution Thread
Dispatch_async (Queue, ^{
NSLog (@ "task 1:%@%d", [Nsthread currentthread],[nsthread currentthread].ismainthread);
});
Parallel queues are obtained through Dispatch_get_global_queue, and the system creates three different priority dispatch queue
To create your own queue
dispatch_queue_t queue = dispatch_queue_create ("Com.class15.comcrrentQueue", dispatch_queue_concurrent);
Dispatch_async (Queue, ^{
NSLog (@ "task 1:%@%d", [Nsthread currentthread],[nsthread currentthread].ismainthread);
});
IOS Development Multi-thread GCD