One, five ways to create multi-threading |
1. How to turn on threading
Nsthread * Thread=[[nsthread alloc] initwithtarget:self selector: @selector (_update) Object:nil];
2. How to turn on threading method two
[Nsthread detachnewthreadselector: @selector (_update) totarget:self Withobject:nil];
3. How to turn on threading three
[Self Performselectorinbackground: @selector (_update) Withobject:nil];
4. How to turn on threading four
Nsoperationqueue *queue=[[nsoperationqueue alloc] init]; [Queue addoperationwithblock:^{for (int i=0;i<50;i++) { printf ("sub-thread \ n"); } ];
5. How to turn on threading five
<pre name= "code" class= "OBJC" > //First step to open thread pool nsoperationqueue * Queue=[[nsoperationqueue alloc] init]; Set number of concurrent [queue setmaxconcurrentoperationcount:2]; Second create multithreaded add to thread pool nsinvocationoperation * thread1=[[nsinvocationoperation alloc] initwithtarget:self selector:@ Selector (_update1) Object:nil]; Nsinvocationoperation *thread2=[[nsinvocationoperation alloc] initwithtarget:self selector: @selector (_UPDATE2) Object:nil]; [Thread1 Setqueuepriority:nsoperationqueuepriorityverylow]; [Thread2 Setqueuepriority:nsoperationqueuepriorityveryhigh]; [Queue addoperation:thread1]; [Queue addoperation:thread2];
Second, multi-threaded application examples, loading pictures. |
1. Core Ideas
Consider that if loading a network picture is delayed, in one mainline preempted will affect the rendering of the control, at which point you can take multi-threading and refresh the UI after the asynchronous loading is complete.
2. Realization of Ideas
Multi-threaded downloads are achieved by adding classes to the Uiimageview.
Main code:
#import "Uiimageview+thread.h" @implementation uiimageview (load)-(void) Setimagewithurl: (NSString *) url{ [self Performselectorinbackground: @selector (_loadimage:) Withobject:url];} -(void) _loadimage: (NSString *) u{ @autoreleasepool { nsurl *url=[nsurl urlwithstring:u]; NSData *data=[nsdata Datawithcontentsofurl:url]; UIImage *image=[uiimage Imagewithdata:data]; [Self Performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no]; }}
Multi-threaded usage in IOS development