Three methods for creating multithreading in iOS

Source: Internet
Author: User

Three methods for creating multithreading in iOS

(1) // use the NSObject method to create a thread// (This method will automatically open up a background thread, parameter 1: The method executed in this background thread, parameter 2: used to pass Parameters) [self defined mselectorinbackground: @ selector (banZhuanPlus) withObject: nil];

(2) // create a thread through NSThread (parameter 1: Method performer; parameter 2: method executed in the thread; parameter 3: used to pass Parameters)
// Step 1: Create a thread
NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (banZhuanPlus) object: nil];
// Step 2: Execute
[Thread start];

[Thread release];


(3) // NSOperation is an operation unit used to execute methods. It is an abstract class and must be subclass or use the NSInvocationOperation or NSBlockOperation class created by the system)
//// NSOperation is the smallest operation unit and can be executed only once;

/// NSInvocationOperation Step 1: Create
NSInvocationOperation * invocation = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (banZhuanPlus) object: nil];
/// Step 2: (if not set, do not add it to the queue)
// [Invocation start];

// NSBlockOperation Step 1: Create
NSBlockOperation * block = [NSBlockOperation blockOperationWithBlock: ^ {
[Self banZhuanPlus];
}];
/// Step 2: Execute (in the main thread)
// [Block start]; // if it is added to the queue, do not start

// This queue will automatically create an auxiliary thread for us
// Only NSOperation and subclass objects can be added to this queue;
NSOperationQueue * queue = [[NSOperationQueue alloc] init];
[Queue setMaxConcurrentOperationCount: 2]; // you can specify the maximum number of parallel rows;
[Queue addOperation: block]; // you only need to add the operation queue to the queue;
[Queue addOperation: invocation];

// Queue: FIFO
// Stack: Advanced and later

// The queue involves serial and parallel
// Serial: Only one task can be executed at a time.
// Parallel execution: multiple tasks can be executed at a time.
(When copying the whole part, note that there is no comment in the whole part)


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.