·NSOperation简介:
1.简单说明
NSOperation的作⽤:配合使用NSOperation和NSOperationQueue也能实现多线程编程
NSOperation和NSOperationQueue实现多线程的具体步骤:
(1)先将需要执行的操作封装到一个NSOperation对象中
(2)然后将NSOperation对象添加到NSOperationQueue中
(3)系统会⾃动将NSOperationQueue中的NSOperation取出来
(4)将取出的NSOperation封装的操作放到⼀条新线程中执⾏
2.NSOperation的子类
NSOperation是个抽象类,并不具备封装操作的能力,必须使⽤它的子类
使用NSOperation⼦类的方式有3种:
(1)NSInvocationOperation
(2)NSBlockOperation
(3)自定义子类继承NSOperation,实现内部相应的⽅法
代码:
1.
//直接start ,是在主线程执行,放在队列中,就在队列的线程中执行
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"thread1==%@",[NSThread currentThread]);
}];
[operation1 start];
控制台输出:
2016-12-20 16:54:22.261 NSOperation[30410:19443376] thread1==<NSThread: 0x600000078140>{number = 1, name = main}
注:number=1才是主线程,number是线程的ID
2.
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"thread1==%@",[NSThread currentThread]);
}];
[queue addOperation:operation1];
控制台输出:
2016-12-20 16:55:39.997 NSOperation[30439:19445283] thread1==<NSThread: 0x608000069780>{number = 3, name = (null)}
3.
NSOperationQueue * queue = [NSOperationQueue mainQueue];
//直接start ,是在主线程执行,放在队列中,就在队列的线程中执行
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"thread1==%@",[NSThread currentThread]);
}];
[queue addOperation:operation1];
控制台输出:
2016-12-20 16:56:36.720 NSOperation[30459:19446751] thread1==<NSThread: 0x60800006d580>{number = 1, name = main}```
4 添加依赖
NSOperationQueue * queue = [[NSOperationQueue alloc]init];
//直接start ,是在主线程执行,放在队列中,就在队列的线程中执行
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
sleep(5);
NSLog(@"thread1==%@",[NSThread currentThread]);
sleep(2);
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(2);
NSLog(@"thread2==%@",[NSThread currentThread]);
}];
NSBlockOperation * blockOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1111111111111");
}];
//添加依赖,前面的依赖后面的,在后面的执行完再执行前面的
//blockOp 依赖operation1 和 operation2,在他们执行完之后再执行blockOp
[blockOp addDependency:operation1];
[blockOp addDependency:operation2];
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:blockOp];
NSLog(@"123433434");
控制台输出:
2016-12-20 17:01:22.929 NSOperation[30530:19451239] 123433434
2016-12-20 17:01:25.003 NSOperation[30530:19451358] thread2==<NSThread: 0x608000264300>{number = 3, name = (null)}
2016-12-20 17:01:28.003 NSOperation[30530:19451359] thread1==<NSThread: 0x608000269fc0>{number = 4, name = (null)}
2016-12-20 17:01:30.079 NSOperation[30530:19451358] 1111111111111
5.注意,在任务里再次开分线程,执行异步方法,添加依赖之后,没有用,同步方法才有用
NSBlockOperation * operation1 = [NSBlockOperation blockOperationWithBlock:^{
//异步方法
dispatch_async(dispatch_get_global_queue(0, 0), ^{
sleep(5);
NSLog(@"thread1==%@",[NSThread currentThread]);
sleep(2);
});
}];
NSBlockOperation * operation2 = [NSBlockOperation blockOperationWithBlock:^{
sleep(2);
NSLog(@"thread2==%@",[NSThread currentThread]);
}];
NSBlockOperation * blockOp = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"1111111111111");
}];
[blockOp addDependency:operation1];
[blockOp addDependency:operation2];
[queue addOperation:operation1];
[queue addOperation:operation2];
[queue addOperation:blockOp];
NSLog(@"123433434");
控制台输出:
2016-12-20 17:06:50.847 NSOperation[30601:19455477] 123433434
2016-12-20 17:06:52.919 NSOperation[30601:19455612] thread2==<NSThread: 0x60800007b140>{number = 3, name = (null)}
2016-12-20 17:06:52.919 NSOperation[30601:19455612] 1111111111111
2016-12-20 17:06:55.922 NSOperation[30601:19455633] thread1==<NSThread: 0x60800007b5c0>{number = 4, name = (null)}
6.添加循环依赖也是没用的,例如a依赖b,b依赖c,c依赖a,互相依赖队列是不会执行的
谢谢大家,感谢胡同学 =。=