) IOS multi-thread development-Explanation of nsoperation

Source: Internet
Author: User

Multi-threaded development requires special care, even for engineers with years of development experience.

To enable junior development engineers to use multiple threads and simplify complexity. Various programming tools provide their respective methods. For iOS, we recommend that you avoid direct thread operations as much as possible, using mechanisms such as nsoperationqueue.

You can regard nsoperationqueue as a thread pool and add operations (nsoperation) to the thread pool to the queue. The thread in the thread pool can be viewed as a consumer, taking the operation from the queue and executing it.

You can set that there is only one thread in the thread pool, so that each operation can be considered to be executed in an approximate sequence. Why is it an approximation? I will explain it later.

 

Write the simplest example

First, write a simple example.

 

Compile a subclass of nsoperation and only implement the main method. This is very similar to the Java thread. You can inherit it and overwrite the run method.Code. The main method and run method have similar functions.

Header file:

@ Interface mytask:Nsoperation{
Int operationid;
}

@ Property int operationid;

@ End

The operationid attribute here is not mandatory. It is the identifier that I want to identify after multiple tasks.

M file:

@ Implementation mytask

@ Synthesize operationid;

-(Void) Main {
Nslog (@ "task % I run... ", Operationid );
[Nsthread sleepfortimeinterval: 10];
Nslog (@ "task % I is finished.", operationid );
}

@ End

Here we simulate an operation that takes 10 seconds.

Add the task to the queue as follows:

-(Void) viewdidload {
[Super viewdidload];
Queue = [[nsoperationqueue alloc] init];

Int Index = 1;
Mytask * task = [[mytask alloc] init] autorelease];
Task. operationid = index ++;

[Queue addoperation: task];

I directly found a controller method and wrote it. The running result is that the interface appears, and the task has not been executed, which indicates that it is multi-threaded. After 10 seconds, the log is printed as follows:

15:59:14. 622 multithreadtest [24271: 6103] Task 1 run...
15:59:24. 623 multithreadtest [24271: 6103] Task 1 is finished.

You can add multiple operations to the operation Queue (nsoperationqueue), for example:

-(Void) viewdidload {
[Super viewdidload];
Queue = [[nsoperationqueue alloc] init];

Int Index = 1;
Mytask * task = [[mytask alloc] init] autorelease];
Task. operationid = index ++;
[Queue addoperation: task];

Task = [[[mytask alloc] init] autorelease];
Task. operationid = index ++;

[Queue addoperation: task];
}

 

Therefore, the printed content is not fixed. It may be like this:

15:49:48. 087 multithreadtest [24139: 6203] Task 1 run...
15:49:48. 087 multithreadtest [24139: 1903] Task 2 run...
15:49:58. 122 multithreadtest [24139: 6203] Task 1 is finished.
15:49:58. 122 multithreadtest [24139: 1903] Task 2 is finished.

It may even be like this:

15:52:24. 686 multithreadtest [24168: 1b03] Task 2 run...
15:52:24. 685 multithreadtest [24168: 6003] Task 1 run...
15:52:34. 708 multithreadtest [24168: 1b03] Task 2 is finished.
15:52:34. 708 multithreadtest [24168: 6003] Task 1 is finished.

 

Because the time interval between the two operations is very close, the thread in the thread pool cannot be started first.

So what should we do if we need to strictly execute them in order?

 

Processing dependencies between operations

If the operation is directly dependent, for example, the second operation must be executed after the first operation is completed, you need to write it as follows:

Queue = [[nsoperationqueue alloc] init];

Int Index = 1;
Mytask * task = [[mytask alloc] init] autorelease];
Task. operationid = index ++;

[Queue addoperation: task];

Task = [[[mytask alloc] init] autorelease];
Task. operationid = index ++;

If ([[queue operations] Count]> 0 ){
Mytask * thebeforetask = [[queue operations] lastobject];
[Task adddependency: thebeforetask];
}

[Queue addoperation: task];

 

In this way, even in the case of multiple threads, we can see that the operations are executed in strict order.

 

Number of threads in the thread pool

You can use the following code:

[Queue setmaxconcurrentoperationcount: 2];

To set the number of threads in the thread pool, that is, the number of concurrent operations. By default, it is-1, that is, there is no limit, and all operations in the queue are run simultaneously.

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.