接着上面的GCD封装,以下进行列子验证
1.导入GCD.h
2.创一个串行队列:
- (void)serailQueue{
//创建出队列
GCDQueue *queue = [[GCDQueue alloc]initSerial];
//运行队列中的线程
[queue execute:^{
NSLog(@"1");
}];
[queue execute:^{
NSLog(@"2");
}];
[queue execute:^{
NSLog(@"3");
}];
[queue execute:^{
NSLog(@"4");
}];
[queue execute:^{
NSLog(@"5");
}];
}
得出串行结果:
2015-07-11 10:34:28.641 GCDDelay[2262:174638] 1
2015-07-11 10:34:28.642 GCDDelay[2262:174638] 2
2015-07-11 10:34:28.642 GCDDelay[2262:174638] 3
2015-07-11 10:34:28.642 GCDDelay[2262:174638] 4
2015-07-11 10:34:28.643 GCDDelay[2262:174638] 5
3.并发队列
- (void)initConcurrent{
GCDQueue *queue = [[GCDQueue alloc]initConcurrent];
//运行队列中的线程
[queueexecute:^{
NSLog(@"1");
}];
[queue execute:^{
NSLog(@"2");
}];
[queue execute:^{
NSLog(@"3");
}];
[queue execute:^{
NSLog(@"4");
}];
[queue execute:^{
NSLog(@"5");
}];
}
得出并发结果:
2015-07-11 10:38:28.849 GCDDelay[2303:178328] 3
2015-07-11 10:38:28.849 GCDDelay[2303:178334] 4
2015-07-11 10:38:28.849 GCDDelay[2303:178325] 2
2015-07-11 10:38:28.849 GCDDelay[2303:178327] 1
2015-07-11 10:38:28.850 GCDDelay[2303:178335] 5
4.globalQueue 是默认级别的子线程 。一般在子线程中处理业务逻辑,主线程中更新UI
[GCDQueue
executeInGlobalQueue:^{
//处理业务逻辑
NSURLRequest *request = [NSURLRequestrequestWithURL:[NSURLURLWithString:@"***"]];
NSData *picData =[NSURLConnectionsendSynchronousRequest:request
returningResponse:nilerror:nil];
self.image = [UIImage imageWithData:picData];
[GCDQueue executeInMainQueue:^{
//主线程中更新UI
self.imageView.image =self.image;
}];
}];
总结:
串行队列一次仅仅运行一个线程,依照加入到队列的顺序依次运行
并发队列一次能够运行多个线程,线程的运行没有先后顺序
UI界面所在的线程队列是串行队列