Nsoperation?: Multi-threaded programming with nsoperation and Nsoperationqueue
Nsoperation and nsoperationqueue The specific steps for Multithreading:
1) First encapsulate the required action into a Nsoperation object
2) then add the Nsoperation object to the Nsoperationqueue
3) The system will move the nsoperation out of the nsoperationqueue.
4) put the removed nsoperation package into the new thread.
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, implement the internal corresponding method
Nsinvocationoperation Open Task method
//用 NSInvocationOperation 创建线程 NSInvocationOperation *invocation = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(download:) object:@"NSInvocationOperation"];//在当前线程中同步执行[invocation start];- (void)download:(NSString *)url{ NSLog(@"%@ --- %@", [NSThread currentThread], url);}
Adds the created task to the queue, asynchronously executes
-(void ) invocationoperation{//create thread with nsinvocationoperation nsinvocationoperation *invoca tion = [[Nsinvocationoperation alloc] Initwithtarget:self selector: @selector (download:) object:@" nsinvocationoperation "]; //Creating a global queue (implementing asynchronous concurrency Execution) Nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; //remove Task from column execution (async) [Queue addoperation:invocation];} -(void ) Download: (nsstring *) url{nslog (@ "%@---%@" , [ currentthread], url);}
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
Nsblockoperation How to open a task
NSBlockOperation 添加任务- (void)blockOperation2{ //创建任务 NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"下载图片1 --- %@", [NSThread currentThread]); }]; [operation1 start];}
Note: The operation is performed asynchronously as long as the nsblockoperation encapsulated operand > 1
- (void)blockOperation2{ //注意:只要NSBlockOperation封装的操作数 > 1,就会异步执行操作 //创建任务 NSBlockOperation *operation1 = [NSBlockOperation blockOperationWithBlock:^{ NSLog(@"下载图片1 --- %@", [NSThread currentThread]); }]; [operation1 addExecutionBlock:^{ NSLog(@"下载图片2 --- %@", [NSThread currentThread]); }]; [operation1 addExecutionBlock:^{ NSLog(@"下载图片3 --- %@", [NSThread currentThread]); }]; [operation1 start];}
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
Get Nsoperationqueue queue:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];//并发队列 NSOperationQueue *mainQueue = [NSOperationQueue mainQueue];//主队列
There are two ways to add a task:
- (void)addOperation:(NSOperation *)op;- (void)addOperationWithBlock:(void (^)(void))block;
Instance Code
- (void) blockoperation{//Create global Queue (for asynchronous concurrent execution)Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Concurrent Queue //nsoperationqueue *mainqueue = [nsoperationqueue mainqueue];//home Row //Create a taskNsblockoperation *operation1 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 1---%@", [NsthreadCurrentThread]); }]; Nsblockoperation *operation2 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download Picture 2---%@", [NsthreadCurrentThread]); }]; Nsblockoperation *operation3 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 3---%@", [NsthreadCurrentThread]); }]; Nsblockoperation *operation4 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 4---%@", [NsthreadCurrentThread]); }]; Nsblockoperation *operation5 = [Nsblockoperation blockoperationwithblock:^{NSLog(@"Download picture 5---%@", [NsthreadCurrentThread]); }];//Add a task to the queue[Queue Addoperation:operation1]; [Queue Addoperation:operation2]; [Queue Addoperation:operation3]; [Queue Addoperation:operation4]; [Queue addoperation:operation5];}
- (void) queueaddblock{//Create global Queue (for asynchronous concurrent execution)Nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//Add tasks directly to the queue[Queue addoperationwithblock:^{NSLog(@"Download picture 1---%@", [NsthreadCurrentThread]); }]; [Queue addoperationwithblock:^{NSLog(@"Download Picture 2---%@", [NsthreadCurrentThread]); }]; [Queue addoperationwithblock:^{NSLog(@"Download picture 3---%@", [NsthreadCurrentThread]); }]; [Queue addoperationwithblock:^{NSLog(@"Download picture 4---%@", [NsthreadCurrentThread]); }]; [Queue addoperationwithblock:^{NSLog(@"Download picture 5---%@", [NsthreadCurrentThread]); }];}
iOS Multithreading--nsoperation Introduction