一,NSOperation中的两种队列
1,主队列:通过mainQueue获得,凡是放到主队列中的任务都将在主线程执行。
2,非主队列:通过alloc init出来的队列。非主队列同时具备了并发和串行的功能,通过设置最大并发数属性来控制任务是并发执行还是串行执行。
二,NSBlockOperation
1,使用方法:
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"---1--%@---", [NSThread currentThread]);
}];
//NSBlockOperation *op1 = [[NSBlockOperation alloc] init];
[op1 addExecutionBlock:^{
NSLog(@"---1.1--%@---", [NSThread currentThread]);
}];
[op1 addExecutionBlock:^{
NSLog(@"---1.2--%@---", [NSThread currentThread]);
}];
[op1 addExecutionBlock:^{
NSLog(@"---1.3--%@---", [NSThread currentThread]);
}];
[op1 addExecutionBlock:^{
NSLog(@"---1.4--%@---", [NSThread currentThread]);
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"---2--%@---", [NSThread currentThread]);
}];
//NSBlockOperation *op2 = [[NSBlockOperation alloc] init];
[op2 addExecutionBlock:^{
NSLog(@"---2.1--%@---", [NSThread currentThread]);
}];
[op2 addExecutionBlock:^{
NSLog(@"---2.2--%@---", [NSThread currentThread]);
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"---3--%@---", [NSThread currentThread]);
}];
//NSBlockOperation *op3 = [[NSBlockOperation alloc] init];
[op3 addExecutionBlock:^{
NSLog(@"---3.1--%@---", [NSThread currentThread]);
}];
[op3 addExecutionBlock:^{
NSLog(@"---3.2--%@---", [NSThread currentThread]);
}];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperationWithBlock:^{
NSLog(@"---addOperationWithBlock--%@---", [NSThread currentThread]);
}];
第一次打印:
2021-09-08 14:57:27.315900+0800 OperationTest[35536:17380983] ---1--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316032+0800 OperationTest[35536:17380983] ---1.1--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316061+0800 OperationTest[35536:17380986] ---3--<NSThread: 0x2820a5000>{number = 3, name = (null)}---
2021-09-08 14:57:27.316077+0800 OperationTest[35536:17380983] ---1.2--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316063+0800 OperationTest[35536:17380985] ---2--<NSThread: 0x2820a8e40>{number = 6, name = (null)}---
2021-09-08 14:57:27.316123+0800 OperationTest[35536:17380983] ---1.3--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316169+0800 OperationTest[35536:17380983] ---1.4--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316194+0800 OperationTest[35536:17380985] ---2.1--<NSThread: 0x2820a8e40>{number = 6, name = (null)}---
2021-09-08 14:57:27.316196+0800 OperationTest[35536:17380986] ---3.1--<NSThread: 0x2820a5000>{number = 3, name = (null)}---
2021-09-08 14:57:27.316218+0800 OperationTest[35536:17380983] ---2.2--<NSThread: 0x2820a6540>{number = 5, name = (null)}---
2021-09-08 14:57:27.316227+0800 OperationTest[35536:17380988] ---addOperationWithBlock--<NSThread: 0x2820a6f00>{number = 7, name = (null)}---
2021-09-08 14:57:27.316292+0800 OperationTest[35536:17380984] ---3.2--<NSThread: 0x2820a6740>{number = 4, name = (null)}---
第n次打印:
2021-09-08 15:02:07.093007+0800 OperationTest[35544:17382382] ---1--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093113+0800 OperationTest[35544:17382382] ---1.1--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093113+0800 OperationTest[35544:17382385] ---2--<NSThread: 0x28104c100>{number = 7, name = (null)}---
2021-09-08 15:02:07.093113+0800 OperationTest[35544:17382384] ---3--<NSThread: 0x28105cd80>{number = 6, name = (null)}---
2021-09-08 15:02:07.093154+0800 OperationTest[35544:17382382] ---1.2--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093193+0800 OperationTest[35544:17382382] ---1.3--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093240+0800 OperationTest[35544:17382382] ---1.4--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093243+0800 OperationTest[35544:17382385] ---2.1--<NSThread: 0x28104c100>{number = 7, name = (null)}---
2021-09-08 15:02:07.093287+0800 OperationTest[35544:17382382] ---addOperationWithBlock--<NSThread: 0x28100d440>{number = 5, name = (null)}---
2021-09-08 15:02:07.093300+0800 OperationTest[35544:17382384] ---3.1--<NSThread: 0x28105cd80>{number = 6, name = (null)}---
2021-09-08 15:02:07.093340+0800 OperationTest[35544:17382387] ---2.2--<NSThread: 0x28105c000>{number = 3, name = (null)}---
2021-09-08 15:02:07.093381+0800 OperationTest[35544:17382383] ---3.2--<NSThread: 0x28105cc40>{number = 4, name = (null)}---
解析:
会开启新线程,并发执行任务。
三,NSInvocationOperation
1,使用方法:
NSInvocationOperation *op1 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation1:) object:@"first"];
NSInvocationOperation *op2 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation2:) object:@"second"];
NSInvocationOperation *op3 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation3:) object:@"third"];
NSInvocationOperation *op4 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation4:) object:@"fourth"];
NSInvocationOperation *op5 = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(operation5:) object:@"fifth"];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[queue addOperation:op1];
[queue addOperation:op2];
[queue addOperation:op3];
[queue addOperation:op4];
[queue addOperation:op5];
- (void)operation1:(id)obj {
NSLog(@"---1--%@--%@---", obj, [NSThread currentThread]);
}
- (void)operation2:(id)obj {
NSLog(@"---2--%@--%@---", obj, [NSThread currentThread]);
}
- (void)operation3:(id)obj {
NSLog(@"---3--%@--%@---", obj, [NSThread currentThread]);
}
- (void)operation4:(id)obj {
NSLog(@"---4--%@--%@---", obj, [NSThread currentThread]);
}
- (void)operation5:(id)obj {
NSLog(@"---5--%@--%@---", obj, [NSThread currentThread]);
}
打印:
第一次打印
2021-09-08 15:22:04.033499+0800 OperationTest[35609:17394083] ---1--first--<NSThread: 0x282aa8f00>{number = 5, name = (null)}---
2021-09-08 15:22:04.033619+0800 OperationTest[35609:17394083] ---3--third--<NSThread: 0x282aa8f00>{number = 5, name = (null)}---
2021-09-08 15:22:04.033645+0800 OperationTest[35609:17394082] ---2--second--<NSThread: 0x282ae1280>{number = 6, name = (null)}---
2021-09-08 15:22:04.033673+0800 OperationTest[35609:17394083] ---4--fourth--<NSThread: 0x282aa8f00>{number = 5, name = (null)}---
2021-09-08 15:22:04.033723+0800 OperationTest[35609:17394083] ---5--fifth--<NSThread: 0x282aa8f00>{number = 5, name = (null)}---
第n次打印
2021-09-08 15:24:49.035754+0800 OperationTest[35629:17395412] ---1--first--<NSThread: 0x281751140>{number = 5, name = (null)}---
2021-09-08 15:24:49.035866+0800 OperationTest[35629:17395412] ---2--second--<NSThread: 0x281751140>{number = 5, name = (null)}---
2021-09-08 15:24:49.035944+0800 OperationTest[35629:17395412] ---5--fifth--<NSThread: 0x281751140>{number = 5, name = (null)}---
2021-09-08 15:24:49.035954+0800 OperationTest[35629:17395413] ---3--third--<NSThread: 0x281750740>{number = 4, name = (null)}---
2021-09-08 15:24:49.035971+0800 OperationTest[35629:17395410] ---4--fourth--<NSThread: 0x28175c2c0>{number = 6, name = (null)}---
解析:
会开启新线程,并发执行任务。
网友评论