The use of nsoperation and Nsoperationqueue under multithreading

Source: Internet
Author: User

The use of nsoperation and Nsoperationqueue under multithreading is described in Nsoperation and nsoperationqueue:

nsoperation is a Cocoa an abstract class used to encapsulate a single task and code to perform an operation, because it is an abstract class, so it cannot be instantiated directly, it must define subclasses to inherit the abstract class to implement, more commonly used nsoperation the subclass has nsinvocationoperation , in addition, you can also inherit nsoperation to implement the operation of the thread.

Nsoperationqueue, it is equivalent to a thread queue or can be called a thread pool. You can sequentially perform operations in the queue, or you can set the priority for operations in the queue.

By using these two classes together, you can implement multithreaded programming.

use of nsoperation:Nsoperation the method of setting events for the two subclasses:
Nsinvocationoperation *operation1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (selector:) Object:nil]; Nsblockoperation *blockoperation = [[Nsblockoperation alloc] init]; [Blockoperation addexecutionblock:^{        NSLog (@ "1st the thread is%@", [Nsthread CurrentThread]);    }]; [Operation1 start];

The first is to use Nsinvocationoperation to set the event.

The second is to use Nsblockoperation to set the event, and the event is set in code open.

The third is a way to open an event of a nsoperation subclass individually.

Use of Nsoperationqueue:
Nsoperationqueue *queue = [[Nsoperationqueue alloc] init]; [Queue Addoperation:operation1]; [Operation1 Adddependency:operation2];

First method: Create and initialize a queue

The second method: Add an event, if the event is added, then the program runs directly

Third method: Sequence setting of events

Use the above method instance
@implementation nsoperationcontroller-(void) viewdidload {[Super viewdidload];    Do any additional setup after loading the view.    [Self.view Setbackgroundcolor:[uicolor Whitecolor]; [Self invocationOperation1];} #pragma mark detail method-(void) invocationoperation1{//use the Nsinvocationoperation class to set events Nsinvocationoperation *opera Tion1 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (logtheoperation:) object:@ "This is the    Frist Invovationopration "]; Nsinvocationoperation *operation2 = [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (    Logtheoperation:) object:@ "This is the second invocationoperation"];    Use Nsblockoperation to set events, and add event nsblockoperation *blockoperation = [[Nsblockoperation alloc] init];    [Blockoperation addexecutionblock:^{NSLog (@ "1st the thread is%@", [Nsthread CurrentThread]);    }]; [Blockoperation addexecutionblock:^{NSLog (@ "2st the thread is%@", [Nsthread CurrentThread]);    }];    [Blockoperation addexecutionblock:^{NSLog (@ "3st the thread is%@", [Nsthread CurrentThread]);    }]; Here we can also set the order of events, such a setting, the order of operation should be Blockoperation->operation1->operation2 [Operation1 adddependency:o    Peration2];        [Blockoperation Adddependency:operation2]; Initialize a queue nsoperationqueue *queue = [[Nsoperationqueue alloc] init];//add events to the queue [queue Addoperation:operatio    N1];    [Queue Addoperation:operation2];    [Queue addoperation:blockoperation];    Another way to store events//[Operation1 start];//[Operation2 start];//[blockoperation start]; }-(void) Logtheoperation: (NSString *) log{NSLog (@ "% @the thread is%@", log, [Nsthread CurrentThread]);} @end

The use of nsoperation and Nsoperationqueue under multithreading

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.