本来想写在上个多线程(二)呢,无奈GCD内容太多,暂时先写那么多
NSOperation
NSOperation 是对GCD的封装,面向对象NSOperation-> {GCD},NSOperationQueue-> dispatch_queue ,由于NSOperation是抽象类,其实现的两种方式: Selector 和 block 执行任务分别对应 NSInvocationOperation 和 NSBlockOperation
直接上代码:
Selector 方式
NSInvocationOperation * invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(methodTest) object:nil];
[invocation start];
NSInvocationOperation * invocation = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(methodTest) object:nil];
[invocation start];
block 方式
NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"线程%@",[NSThread currentThread]);
}];
//多个block 之间会并发执行,在在主线程和其它的多个线程
for (int i = 0; i < 5; i++) {
[operation addExecutionBlock:^{
NSLog(@"线程%@",[NSThread currentThread]);
}];
}
[operation start];
/**
2017-11-07 18:19:44.899 多线程测试[12298:367880] 线程<NSThread: 0x60800007bc80>{number = 5, name = (null)}
2017-11-07 18:19:44.899 多线程测试[12298:367883] 线程<NSThread: 0x600000078440>{number = 4, name = (null)}
2017-11-07 18:19:44.899 多线程测试[12298:367881] 线程<NSThread: 0x60800007bc40>{number = 3, name = (null)}
2017-11-07 18:19:44.900 多线程测试[12298:367883] 线程<NSThread: 0x600000078440>{number = 4, name = (null)}
2017-11-07 18:19:44.900 多线程测试[12298:367880] 线程<NSThread: 0x60800007bc80>{number = 5, name = (null)}
2017-11-07 18:19:44.899 多线程测试[12298:367781] 线程<NSThread: 0x60800006e7c0>{number = 1, name = main}
*/
队列(自动start)
主队列
NSOperationQueue * queue = [NSOperationQueue mainQueue];
其它队列,全部子线程
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
测试
NSBlockOperation * operation = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"线程%@",[NSThread currentThread]);
}];
for (int i = 0; i < 5; i++) {
[operation addExecutionBlock:^{
NSLog(@"线程%@",[NSThread currentThread]);
}];
}
//[queue setMaxConcurrentOperationCount:1];//设置最大并发数,类似于串行
[queue addOperation:operation];
/**
2017-11-07 18:24:55.277 多线程测试[12356:370699] 线程<NSThread: 0x608000268700>{number = 6, name = (null)}
2017-11-07 18:24:55.277 多线程测试[12356:370685] 线程<NSThread: 0x600000263200>{number = 4, name = (null)}
2017-11-07 18:24:55.277 多线程测试[12356:370682] 线程<NSThread: 0x6080002685c0>{number = 3, name = (null)}
2017-11-07 18:24:55.277 多线程测试[12356:370683] 线程<NSThread: 0x600000263800>{number = 5, name = (null)}
2017-11-07 18:24:55.278 多线程测试[12356:370699] 线程<NSThread: 0x608000268700>{number = 6, name = (null)}
2017-11-07 18:24:55.278 多线程测试[12356:370685] 线程<NSThread: 0x600000263200>{number = 4, name = (null)}
*/
设置依赖
NSBlockOperation * block1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"线程1 - %@", [NSThread currentThread]);
}];
NSBlockOperation * block2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"线程2 - %@", [NSThread currentThread]);
}];
NSBlockOperation * block3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"线程3 - %@", [NSThread currentThread]);
}];
//Block3,block1,block2
[block1 addDependency:block3];
[block2 addDependency:block1];
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
/*
[block3 removeDependency:block1];
*/ //解除依赖
[queue addOperations:@[block1,block2,block3] waitUntilFinished:YES];
NSLog(@"%@,测试",[NSThread currentThread]); //决定于上局代码是否为YES
/**
2017-11-07 18:41:16.621 多线程测试[12510:379366] 线程3 - <NSThread: 0x608000262ac0>{number = 3, name = (null)}
2017-11-07 18:41:16.621 多线程测试[12510:379355] 线程1 - <NSThread: 0x600000266f00>{number = 4, name = (null)}
2017-11-07 18:41:16.622 多线程测试[12510:379355] 线程2 - <NSThread: 0x600000266f00>{number = 4, name = (null)}
*/
总结:
1、最大特色就是可以设置依赖和最大并发数
2、可以通过waitUntilFinish 阻塞线程
3、封装了GCD,使用起来简单