| One, five ways to create multi-threading |
1. How to turn on threading
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<; i++ { printf (" sub-thread \ n");} }];
5. How to turn on threading five
//The first step is to turn on the thread poolNsoperationqueue * queue=[[Nsoperationqueue alloc] init]; //set number of concurrent[Queue Setmaxconcurrentoperationcount:2]; //Second Create multithreaded add to thread poolNsinvocationoperation * 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:
" uiimageview+thread.h " @implementation uiimageview (load)-(void) Setimagewithurl: (NSString *) url{ [self Performselectorinbackground: @selector (_loadimage:) Withobject:url];} -(void) _loadimage: (NSString *) u{ @autoreleasepool { *url=[Nsurl urlwithstring:u]; *data=[NSData Datawithcontentsofurl:url]; *image=[UIImage imagewithdata:data]; [Self Performselectoronmainthread: @selector (setimage:) withobject:image waituntildone:no]; }}
Multi-threaded usage in IOS development