Nsoperation is the GCD, OC language, more simple and convenient
Nsoperation and Nsoperationqueue can also be used for multithreaded programming
Basic steps:
- Encapsulates an action into a Nsoperation object
- To add a Nsoperation object to the Nsoperationqueue queue
- The system will remove the nsoperation from the Nsoperationqueue
- Put the removed Nsoperation encapsulated operation into a new thread to execute
Nsoperation is an abstract class that does not have the ability to operate and must use its subclasses:
- Nsinvocationoperation
- Nsblockoperation (most used)
- Custom subclasses inherit from Nsoperation and implement corresponding methods (less use)
1.NSInvocationOperation:
-(void) invocationoperation{ //Create queue, queue created by Alloc Init is a concurrent queue nsoperationqueue *queue = [[Nsoperationqueue ALLOC] init];
for (int i = 0; i < 5; i++) { //create operation nsinvocationoperation *op = [[Nsinvocationoperation alloc] Initwithtarg Et:self selector: @selector (test) object:nil]; Put the operation in the queue [Addoperation:op]; } } -(void) test{ NSLog (@ "Current thread =%@", [Nsthread CurrentThread]);}
Print to verify that the queue is a concurrent queue
2.NSBlockOperation
-(void) blockoperation{ //Create queue, queue created by Alloc init mode is concurrent queue nsoperationqueue *queue = [[Nsoperationqueue alloc] INIT]; for (int i = 0; i < 5; i++) { //create operation nsblockoperation *op = [Nsblockoperation blockoperationwithblock:^{
nslog (@ "Current thread =%@", [Nsthread CurrentThread]); }]; Put the operation in the queue [Addoperation:op]; }}
This can also be used (simpler)
-(void) blockoperationeasy{ //Create queue, queue created by Alloc Init is a concurrent queue nsoperationqueue *queue = [[Nsoperationqueue ALLOC] init]; [Queue addoperationwithblock:^{ NSLog (@ "Current thread =%@", [Nsthread CurrentThread]); }];}
Add an extra action
-(void) blockoperation{ //Create queue, queue created by Alloc init mode is concurrent queue nsoperationqueue *queue = [[Nsoperationqueue alloc] INIT]; Create Operation nsblockoperation *op = [nsblockoperation blockoperationwithblock:^{ NSLog (@ "Current thread =%@", [Nsthread CurrentThread]); }]; Add another Operation [op addexecutionblock:^{ NSLog (@ "This is an extra operation, thread =%@", [Nsthread CurrentThread]); }]; Put the operation in the queue [Addoperation:op]; }
Nsoperationqueue:
The nsoperationqueue does not have a serial queue, but it can get the home row, get to the main queue with the code below, and the tasks added to the main queue will be executed in the primary thread
[Nsoperationqueue Mainqueue]
Inter-thread communication:
-(void) refreshui{ nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; [Queue addoperationwithblock:^{ NSLog (@ "operations performed in multi-threading%@", [Nsthread CurrentThread]); [[Nsoperationqueue Mainqueue] addoperationwithblock:^{ NSLog (@ "Back to main thread update ui%@", [Nsthread CurrentThread]); }]; }] ;}
Maximum number of concurrent:
- Concurrent numbers are the number of simultaneous tasks performed
- For example, the simultaneous opening of 3 threads to perform 3 tasks, the number of concurrency is 3
- Nsoperation can control the number of operations performed at the same time by the maximum number of concurrent
-(void) max{ nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; Queue.maxconcurrentoperationcount = 2; for (int i = 0; i<; i++) { [queue addoperationwithblock:^{ NSLog (@ "i =%d, thread =%@", I,[nsthread C Urrentthread]);} ];} }
The result is a 2,3,4,5 of four threads, because the maximum concurrency determines the number of operations performed at the same time, not the number of threads;
To suspend a queue:
- The hang-up queue is a task that is saved in its current state and can then continue
- Pending operations do not affect tasks that are already executing
-(void) suspended{ nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; for (int i = 0; i<; i++) { if (i = = 5) { //suspend queue queue.suspended = YES; Delay 2s [nsthread sleepfortimeinterval:2]; Continue queue.suspended = NO; } [Queue addoperationwithblock:^{ NSLog (@ "i =%d, thread =%@", I,[nsthread CurrentThread]);} ];} }
Cancel Operation
- Canceling the operation does not affect the pending state of the queue
- (If the queue is pending) generally cancels the operation and puts the queue in a non-suspended state for easy follow-up
- Suspends the queue, the tasks in the queue exist, can continue, cancel the operation, the task in the queue is gone, only the task can be re-added
-(void) cancel{ nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; Set maximum concurrent number to 1 queue.maxconcurrentoperationcount = 1; for (int i = 0; i<; i++) { [queue addoperationwithblock:^{ [Nsthread sleepfortimeinterval:1]; NSLog (@ "i =%d, thread =%@", I,[nsthread CurrentThread]); }]; } Cancel all operations in the queue after 3S [Nsthread sleepfortimeinterval:3]; [Queue cancelalloperations];}
The printout shows that the operation has been canceled.
Dependent Relationships :
- Dependencies can span queues
- Be careful not to loop dependencies
-(void) dependecy{ nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; Nsblockoperation *one = [nsblockoperation blockoperationwithblock:^{ NSLog (@ "Download music"); }]; Nsblockoperation *two = [nsblockoperation blockoperationwithblock:^{ NSLog (@ "open music"); }]; Nsblockoperation *three = [nsblockoperation blockoperationwithblock:^{ NSLog (@ "play Music"); }]; Add task dependency [Adddependency:one]; [Three adddependency:two]; Wait for the task to complete to continue to the next task, similar to the dispatch group in GCD [queue addoperations:@[one,two,three] waituntilfinished:yes]; NSLog (@ "Relax after listening to music");}
Multithreading (Fri) nsoperation