最大并发数
// 1.创建一个队列(非主队列)
NSOperationQueue
*queue = [[NSOperationQueue
alloc]
init];
// 2.设置最大并发(最多同时并发执行3个任务)
queue.maxConcurrentOperationCount
=
3;
// 3.添加操作到队列中(自动异步执行任务,并发)
NSBlockOperation
*operation1 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"下载图片1---%@",
[NSThread
currentThread]);
}];
NSBlockOperation
*operation2 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"下载图片2---%@",
[NSThread
currentThread]);
}];
NSBlockOperation
*operation3 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"下载图片3---%@",
[NSThread
currentThread]);
}];
NSBlockOperation
*operation4 = [NSBlockOperation
blockOperationWithBlock:^{
NSLog(@"下载图片4---%@",
[NSThread
currentThread]);
}];
NSInvocationOperation
*operation5 = [[NSInvocationOperation
alloc]
initWithTarget:self
selector:@selector(download)
object:nil];
[queue
addOperation:operation1];
[queue
addOperation:operation2];
[queue
addOperation:operation3];
[queue
addOperation:operation4];
[queue
addOperation:operation5];
[queue
addOperationWithBlock:^{
NSLog(@"下载图片5---%@",
[NSThread
currentThread]);
}];
[queue
addOperationWithBlock:^{
NSLog(@"下载图片6---%@",
[NSThread
currentThread]);
}];
[queue
addOperationWithBlock:^{
NSLog(@"下载图片7---%@",
[NSThread
currentThread]);
}];
[queue
addOperationWithBlock:^{
NSLog(@"下载图片8---%@",
[NSThread
currentThread]);
}];
[queue
addOperationWithBlock:^{
NSLog(@"下载图片9---%@",
[NSThread
currentThread]);
}];
[queue
cancelAllOperations];