1.NSThread
The official description
An Nsthread object controls a thread of execution. With this class, you want to a Objective-c method run in its own thread of execution. Threads is especially useful when you need to perform a lengthy task, but don ' t want it to block the execution of the Res T of the application.
Nsthread can control the execution of a thread, using this class when you want to execute the OC method on your own thread. This is also useful when performing long tasks, and does not block the tasks that are left in the program that need to be performed.
Nsthread *thread = [[Nsthread alloc]initwithtarget:self selector:nsselectorfromstring (@ "myThread: " Object : nil]; Start thread [thread start];
Stopping a thread
if (![ Thread iscancelled]) {
[Thread Cancel];
// }
1-(void) MyThread: (ID) sender{2NSLog (@"%@", sender);3 @synchronized (self) {4 while(true) {5[Nsthread Sleepfortimeinterval:1];6 Static inti =0;7NSLog (@"%i", i++);8 if(i = =Ten) {9 [Nsthread exit];Ten } One } A } -}View Code
Result: The thread is terminated only when it is printed to 3 o'clock
:
You can also use this method to start a thread, but it cannot be the default thread configuration
[Nsthread detachnewthreadselector:nsselectorfromstring (@ "myThread:") totarget:self Withobject:@ "myThread"];
Also, this method is defined in NSObject, as long as it is inherited nsobject can be used
[Self performselectorinbackground:nsselectorfromstring (@ "myThread:") Withobject: @ "myThread"];
2.NSOperation
At present my understanding is a encapsulation operation of an operation, and then call its Start method, on the main thread to execute !!!
If you are not in the main thread execution, you can create a nsoperationqueue and then add the action to it to execute
It has two sub-classes Nsblockoperation and Nsinvocationoperation
Nsblockoperation
The NSBlockOperation class is a concrete subclass of this NSOperation manages the concurrent execution of one or more blocks. You can use the This object to execute several blocks at once without have to create separate operation objects for each. When executing to than one block, the operation itself is considered finished if all blocks has finished Executi Ng.
As can be seen, nsblockoperation is the management of multiple block blocks, and only the block has been executed to become finished state;
NSLog (@"%@ MainT =", [Nsthread CurrentThread]); Nsblockoperation*blockop = [Nsblockoperation blockoperationwithblock:^{ for(inti =0; i<5; i++) {[Nsthread sleepfortimeinterval:1]; NSLog (@"block1>>>%i thread =%@", I, [Nsthread CurrentThread]); } }]; [Blockop Addexecutionblock:^{ for(inti =0; i<Ten; i++) {[Nsthread sleepfortimeinterval:1]; NSLog (@"block2>>>%i thread =%@", I, [Nsthread CurrentThread]); } }]; [Blockop start]; NSLog (@"Here we go.");
Running Result: a block is on the main thread, and more than one block is executed in parallel
Nsinvocationoperation
The NSInvocationOperation class is a concrete subclass of this NSOperation manages the execution of a single encapsulated task specified as an I Nvocation. You can use the this class to initiate an operation, consists of invoking a selector on a specified object. This class implements a non-concurrent operation.
Visible, only one operation can be added via Action-target mode
NSLog (@"%@ mainthread =", [Nsthread CurrentThread]); Nsinvocationoperation*invocationoperation1= [[Nsinvocationoperation alloc] initwithtarget:self selector: @selector (myoperation:)Object:@"InvocationOperation1"]; [InvocationOperation1 start]; NSLog (@"Here we go.");-(void) Myoperation: (ID) sender{Static inti =0; while(i<4) {[Nsthread sleepfortimeinterval:1]; [Nsthread Ismainthread]; NSLog (@"I am a thread%@%i", [Nsthread CurrentThread], i++); }}
Operation Result:
-- A- + A: $:23.038mythread[4942:44405] <nsthread:0x7ffcba50c190>{number =1, name = main} Mainthread = -- A- + A: $:24.043mythread[4942:44405] I am a thread <nsthread:0x7ffcba50c190>{number =1, name = main}0 -- A- + A: $:25.046mythread[4942:44405] I am a thread <nsthread:0x7ffcba50c190>{number =1, name = main}1 -- A- + A: $:26.051mythread[4942:44405] I am a thread <nsthread:0x7ffcba50c190>{number =1, name = main}2 -- A- + A: $:27.053mythread[4942:44405] I am a thread <nsthread:0x7ffcba50c190>{number =1, name = main}3 -- A- + A: $:27.054mythread[4942:44405] here it is.
iOS Threading Learning (i)