首页 > 代码库 > GCD编程-串行队列与并发队列
GCD编程-串行队列与并发队列
- (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
- (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;
}];
}];
GCD编程-串行队列与并发队列