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 );}}