Multithreading technology NSThread & amp; NSOperation & amp; GCD, multithreading technology nsthread

Source: Internet
Author: User

Multithreading technology NSThread & NSOperation & GCD, multithreading technology nsthread

 

Multithreading: in iOS development, there are many problems when multithreading is used, such as refreshing the interface when downloading data asynchronously.

The key to introducing multithreading to solve the problem is that using multithreading can make the interface smoother and prevent the interface from being suspended.

The interface is suspended. For example, you can click a button to start a thread, but the thread processing time is 10 s. If you click the button again before the thread execution is complete, therefore, multithreading is introduced to solve the problem.

1. NSThread each NSThread object corresponds to a thread with a lighter magnitude (real multithreading)
The following two technologies are specifically developed by Apple, so that programmers can no longer care about the specific use of threads;
2. NSOperation object-oriented thread technology;
3. GCD-Grand Central Dispatch is a C-language-based framework that can fully utilize multiple cores. It is a multithreading technology recommended by Apple, I personally think it is a block-based multi-thread technology in iOS.

(1 ).

NSThread thread creation method:

(Void) detachNewThreadSelector :( SEL) selector toTarget :( id) target withObject :( id) argument;

(Id) initWithTarget :( id) target selector :( SEL) selector object :( id) argument;

Parameter description:

Selector: the method of thread execution. Only one parameter is allowed and no return value is allowed.

Target: the object of the selector message.

  Argument: the unique parameter transmitted to the target. It can also be nil.

  // Member Method

  NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (bigDemo) object: nil];

  // Start the start thread

  [Thread start];

(2 ).

Two NSOperation subclasses
NSInvocationOperation
NSBlockOperation
Working principle:
Encapsulate the operations to be executed with NSOperation
Put the created NSOperation object in NSOperationQueue
Start OperationQueue and start a new thread to execute operations in the queue
Note:
When multithreading is used, you often need to control the number of concurrent threads, because the threads consume system resources and run too many threads at the same time, and the system slows down.
Use the following method to control the number of concurrent threads:

NSOperation is implemented in NSOperationQueue. This thread creation method is mostly used by developers who do not know multithreading.

Next I will briefly introduce NSOperationQueue:

Although the name of the so-called NSOperationQueue is also a Queue, in fact, it is actually not executed in strict FIFO order of the Queue, So I generally call it a thread pool, the mechanism itself is equivalent to that you go to the bank to handle the business. Maybe you arrive at the bank earlier, but when you queue up, the other person is later than you, however, when you go to different service windows to handle business, you do less business than the person you arrive at the bank, and you do more business, therefore, he will leave the bank first, so I think it can be understood that NSOperationQueue is a thread pool.

  • It is important that the main thread be returned if NSOperation is used to create a thread. The method for returning the main thread is: descrimselecdomainmainthread: @ selector (func) withObject: waitUntilDone:

(3 ).

There are two methods to implement GCD.

There are two steps to use the thread queue,

Step 1: Create a thread queue

Step 2: asynchronous execution thread queue 

The second method uses a thread group (commonly used, when a thread group contains a method to notify the main thread). There are three steps.

Step 1: Create a thread group

Step 2: Create a thread queue

Step 3: place the thread queue to the thread group type and asynchronously execute the thread group

Method 1: thread queue

/01 create a thread queue

Dispatch_queue_t thread = dispatch_queue_create (NULL, NULL );

// 02 asynchronous execution thread Queue (that is, what to do in this thread Queue)

Dispatch_async (thread, ^ {

Sleep (2 );

NSLog (@ "sleep 2 s ");

});

// You can also use dispatch_get_global_queue (0, 0) to obtain the idle thread queue. If not, an on-site queue is automatically created.

Dispatch_async (dispatch_get_global_queue (0, 0), ^ {

Sleep (2 );

NSLog (@ "sleep 2 s ");

});

Method 2: Line

// 01 create a thread group

Dispatch_group_t threadGroup = dispatch_group_create (); 

/02 create a thread queue

Dispatch_queue_t t = dispatch_queue_create (NULL, NULL );

// 03 put the thread queue into the thread group,

Dispatch_group_async (threadGroup, t, ^ {

Sleep (2 );

NSLog (@ "sleep 2 s ");

});

// Idle thread queue can be used here

Dispatch_group_async (threadGroup, dispatch_get_global_queue (0, 0), ^ {

Sleep (2 );

NSLog (@ "sleep 2 s ");

});

/*

One advantage of using a thread group is that the main thread can be notified after all thread queues in the thread group have been executed.

*/

Dispatch_group_notify (threadGroup, dispatch_get_main_queue (), ^ {

NSLog (@ "the thread queue of the thread group has been fully executed. Back to main thread ");

});

// Call the notification only after all the thread queues in the thread group are executed

// The notification is started only after all the thread queues in the thread group are executed.

Dispatch_group_async (threadGroup, dispatch_get_global_queue (0, 0), ^ {

Sleep (3 );

NSLog (@ "sleep 3 s ");

});

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.