Wednesday, January 15, 2014

NSOperationQueue added operation is not the first in first out of it, I discovered how to run it at random

Code is as follows:
 

# Import "OperationTest.h"

# Define EMPTY_LOOP (count) for (int i = 0; i @ Implementation OperationTest
- (Void) operationTest
{
NSInvocationOperation * invocation1 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (dosomething1) object: nil];

NSInvocationOperation * invocation2 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (dosomething2) object: nil];
NSInvocationOperation * invocation3 = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (dosomething3) object: nil];

NSOperationQueue * queue = [[NSOperationQueue alloc] init];

[Queue setMaxConcurrentOperationCount: 3];
[Queue addOperation: invocation1];
[Queue addOperation: invocation2];
[Queue addOperation: invocation3];

}
- (Void) dosomething1
{
NSLog (@ "do something 1 start");
EMPTY_LOOP (1000);
NSLog (@ "do something 1 end");
}
- (Void) dosomething2
{
NSLog (@ "do something 2 start");
EMPTY_LOOP (1000);
NSLog (@ "do something 2 end");
}
- (Void) dosomething3
{
NSLog (@ "do something 3 start");
EMPTY_LOOP (1000);
NSLog (@ "do something 3 end");
}
@ End



The result after running maps:


Execution of three sequential method is not fixed, each different. If a first-in first-out rule, should not be sequential start1, start2, start3 it? Solving.
Reply:
[Queue setMaxConcurrentOperationCount: 3];
Not allow concurrent sentence of three it?
Let the order of execution should be like this:
[Queue setMaxConcurrentOperationCount: 1];
Reply:
cited a floor Murder9527 reply:
[queue setMaxConcurrentOperationCount: 3];
Not allow concurrent sentence of three it?
Let the order of execution should be like this:
[Queue setMaxConcurrentOperationCount: 1];

Yes, I did not understand this sentence, always thought it was only put up in this queue have operation number. Now understand, I can put in a queue in the 10 operation, and then set the maximum number of concurrent to 2, then started the first two opration, these two do not perform random order, if one executed, or while performing complete, will begin in the next two, third, or fourth, or while performing the first three or four. Time on until 10 completed.
Reply:
The results to prove it. A total of six operation, concurrent number 2.
references
do something 2 start
do something 1 start
do something 2 end
do something 3 start
do something 1 end
do something 3 end
do something 4 start
do something 4 end
do something 5 start
do something 5 end
do something 6 start
do something 6 end

No comments:

Post a Comment