I. Introduction of Nsoperation
1.NSOperation: Multi-threaded programming is possible with nsoperation and Nsoperationqueue
Nsoperation and nsoperationqueue The specific steps for Multithreading:
(1) Encapsulate the action that needs to be performed into a Nsoperation object first
(2) Then add the Nsoperation object to the Nsoperationqueue
(3) The system will move the nsoperation out of the nsoperationqueue.
(4) Put the operation of the removed Nsoperation package into the new thread?
Sub-class of 2.NSOperation
Nsoperation is an abstract class, and does not have the ability to encapsulate operations, it must make it a subclass of
There are 3 ways to use the Nsoperation class:
(1) nsinvocationoperation
(2) Nsblockoperation
(3) Customize the subclass inheritance Nsoperation, and implement the internal corresponding method.
Ii. Specific Instructions
1.NSInvocationOperation sub-class
To create an object and perform an action:
1 //Create an Action object, encapsulate the task to be performed 2 //nsinvocationoperation encapsulation Operation 3 nsinvocationoperation *operation=[[ Nsinvocationoperation alloc]initwithtarget:self selector: @selector (Test) object:nil];4 5 //Perform action 6 [ Operation start];
Note: Once the operation is done, the target's test method is called
8 code example: 9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation Yyviewcontroller (void) ViewDidLoad18 { [super viewdidload];20] //nsoperation: Abstract class, no encapsulation function Create an Action object that encapsulates the task to be performed //nsinvocationoperation package Operation Nsinvocationoperation *operation=[[ Nsinvocationoperation alloc]initwithtarget:self selector: @selector (test) object:nil];26 //Perform action 28 [Operation Start];29}31-(void) test33 {NSLog (@ "--test--%@--", [Nsthread CurrentThread]); }37 @end
Print View:
Note: The action object is executed by default in the main thread, and only new threads are opened when added to the queue. That is, by default, if the action is not placed in the queue, it is performed synchronously.
Operations are performed asynchronously only if the nsoperation is placed in a nsoperationqueue
2.NSBlockOperation sub-class
To create an object and add an action:
1 //Create Nsblockoperation Action Object 2 nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{3 //...... 4 }]; 5 6 //Add Operation 7 [Operation addexecutionblock:^{8// .... 9 }];
Code Listing 1:
9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation YYViewController16 17- (void) ViewDidLoad18 { [super viewdidload];20 //Create Nsblockoperation Action object ] Nsblockoperation *operation=[nsblockoperation blockoperationwithblock:^{23 NSLog (@ "nsblockoperation------%@ ", [Nsthread CurrentThread]); }];25 //Open Execution [operation Start];29}30 @end
Print View:
Code Listing 2:
9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation YYViewController16 17- (void) ViewDidLoad18 { [super viewdidload];20] //Create Nsblockoperation Action Object- Nsblockoperation * Operation=[nsblockoperation blockoperationwithblock:^{23 NSLog (@ "nsblockoperation------%@", [Nsthread CurrentThread]); }];25 //Add Operation [Operation Addexecutionblock:^{28 NSLog (@ " NSBlockOperation1------%@ ", [Nsthread CurrentThread]); }];30 [Operation addexecutionblock:^{ NSLog (@ "NSBlockOperation2------%@", [Nsthread CurrentThread]); }];34 //Open Action 36 [Operation Start];37}38 @end
Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1
3.NSOperationQueue
Nsoperationqueue: The nsoperation can be adjusted to start the task, but the default is synchronous execution
If you add nsoperation to the Nsoperationqueue (action queue), the system automatically executes the operations in Nsoperation asynchronously
Add actions to Nsoperationqueue, automate actions, automatically open threads
1 //Create Nsoperationqueue 2 nsoperationqueue * Queue=[[nsoperationqueue Alloc]init]; 3 //Add operation to queue 4 // The first way 5 [queue Addoperation:operation1]; 6 [Queue Addoperation:operation2]; 7 [Queue Addoperation:o Peration3]; 8 //Second way 9 [queue addoperationwithblock:^{10 NSLog (@ "nsblockoperation3--4----%@", [Nsthread CurrentThread]);
-(void) Addoperation: (Nsoperation *) op;
-(void) Addoperationwithblock: (void (^) (void)) block;
code example
9 #import "YYViewController.h" @interface Yyviewcontroller () @end14 @implementation YYViewController16 17- (void) ViewDidLoad18 {[Super viewdidload];20 21//Create Nsinvocationoperation object, package operation: Nsinvocationoperation *o Peration1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1) object:nil];23 Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test2) OBJECT:NIL];24//Create object, package operation Nsblockoperation *operation3=[nsblockoperation blockoperationwithblock:^{26 N SLog (@ "nsblockoperation3--1----%@", [Nsthread CurrentThread]);}];28 [Operation3 addexecutionblock:^{29 N SLog (@ "nsblockoperation3--2----%@", [Nsthread CurrentThread]); 30}];31 32//Create NSOperationQueue33 Nsoperatio Nqueue * Queue=[[nsoperationqueue alloc]init];34//Add operation to queue [queue addoperation:operation1];36 [Queue Addop eration:operation2];37 [Queue Addoperation:operation3];38}39-(void) test141 {NSLog (@ "nsinvocationoperation--test1--%@", [Nsthread CurrentThread]),}44-(voi D) test246 {NSLog (@ "nsinvocationoperation--test2--%@", [Nsthread CurrentThread]);}49 @end
Printing effect:
Note: The system automatically takes out the Nsoperation object in the Nsoperationqueue and puts its encapsulated operation into a new thread to execute. In the preceding code example, there are four tasks,
Operation1 and Operation2 each have a task, operation3 have two tasks. Altogether four tasks, four threads were opened. The time to execute the task is all 273, as you can see,
These tasks are performed in parallel.
Note: The queue is out of order, and print results are not contradictory. This is like, the player A,BC although the starting order is first A, then B, then C, but the order to reach the end is not necessarily a B in front, c in the rear.
The following uses a for loop to print, it is more obvious that the task is executed concurrently.
code example:
9-(void) VIEWDIDLOAD10 {One-to-one [super viewdidload];12 13///Create Nsinvocationoperation object, package operation: nsinvocationoperation *operation1=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test1) object:nil];15 Nsinvocationoperation *operation2=[[nsinvocationoperation alloc]initwithtarget:self selector: @selector (test2) OBJECT:NIL];16//Create object, package operation Nsblockoperation *operation3=[nsblockoperation blockoperationwithblock:^{18 F or (int i=0; i<5; i++) {NSLog (@ "nsblockoperation3--1----%@", [Nsthread CurrentThread]); 20}21 }];22 [Operation3 addexecutionblock:^{23 for (int i=0; i<5; i++) {NSLog (@ "nsblockoperation3--2- ---%@ ", [Nsthread CurrentThread]); 25}26}];27 28//Create NSOperationQueue29 Nsoperationqueue * queue=[ [Nsoperationqueue alloc]init];30//Add operation to queue [queue addoperation:operation1];32 [Queue Addoperation:operatio n2];33 [Queue addoperation:operation3];34}35(void) test137 {$ for (int i=0; i<5; i++) {NSLog (@ "nsinvocationoperation--test1--%@", [Nsthread CURRENTTH Read]);}41}42-(void) test244 {$ for (int i=0; i<5; i++) {$ NSLog (@ "nsinvocationoperation--test2--% @ ", [Nsthread CurrentThread]);}48}49 @end
Four: Multithreading--nsoperation simple Introduction