Multi-thread NSOpertionQueue operation queue, nsoperationqueue

Source: Internet
Author: User

Multi-thread NSOpertionQueue operation queue, nsoperationqueue

// NSOpertionQueue NSOperation // Queue // The main Queue column and custom Queue // The main Queue column run in the main thread, the custom queue runs in the background // NSOperation defines the operations (tasks) to be executed // defines the required operations, then add the operation to the appropriate queue // three steps // 1. create a queue object // 2. create operation object // 3. add the operation object to the queue and wait for the queue allocation thread to execute the operation // 1. create a queue NSOperationQueue * queue = [[NSOperationQueue alloc] init]; // set the maximum number of concurrent operations in the queue. maxConcurrentOperationCount = 1; // whether to suspend the thread in the execution queue [queue setsuincluded: YES]; // 2. the create operation // NSOperation cannot be used directly // two methods of using subclass objects 1. directly create 2. Use block to create NSOperation * op1 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (thread1 :) object: @ "op1"]; NSOperation * op2 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (thread2 :) object: @ "op2"]; /* NSBlockOperation * op3 = [[NSBlockOperation alloc] init]; [op3 addExecutionBlock: ^ {// specific operation to be executed}]; * // 3. add the operation to the queue [queue addOperation: op1]; [queue addOperation: op2]; // after adding the operation, the queue will automatically execute the operation. // 4. set the operation Priority [op1 setQueuePriority: NSOperationQueuePriorityLow]; [op2 setQueuePriority: NSOperationQueuePriorityVeryHigh]; // 5. set the dependency between operations [op2 addDependency: op1]; // op2 execution depends on op1, ensure op1 is sure to execute before op2 // whether to re-execute the queue [queue setsuincluded: NO]; // return to the main thread to print the output for (int I = 0; I <50; I ++) {NSLog (@ "main thread: % d ", i) ;}}-(void) thread1 :( NSString *) name {// specific operation to be executed for (int I = 0; I <50; I ++) {NSLog (@ "multithreading % @: % d", name, I) ;}}-(void) thread2 :( NSString *) name {for (int I = 0; I <50; I ++) {NSLog (@ "multithreading % @: % d", name, I );}}

 

Related Article

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: info-contact@alibabacloud.com 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.