This article reprinted from the Doodle Night Young blog: http://www.cnblogs.com/huluo666/p/3645889.html, modified some code and stickers, if there is infringement of copyright please contact me to delete.
Multithreading This concept of contact is pretty early time, when the single-core single CPU, the concept of thread has emerged, then the more popular scenario is the time slice rotation, the thread can be prioritized preemption, but only one thread at a time, in fact, multithreading is not really parallel processing, Just the macro performance of multithreading in parallel. Now the hardware has progressed a lot, multicore CPU era is coming, so the thread began the real meaning of parallel processing, multithreading also as a more and more important part of the need to master.
There are 3 ways to create and run threads in iOS: Nsthread,nsoperation and GCD. These three ways are more abstract, so writing code is more and more simple.
Let's see Nsthread first.
Nsthread are more lightweight than the other two, but need to manage the lifecycle and synchronization of threads themselves, and code is more complex to write than the other two. For threading Technology, Apple's official document gives a table:
|
Technology |
Description |
|
Cocoa Threads |
Cocoa implements threads using the NSThread class. Cocoa also provides methods on for NSObject spawning new threads and executing code on already-running threads. For the information, see "Using Nsthread" and "using NSObject to Spawn a Thread." |
|
POSIX Threads |
POSIX threads provide a c-based interface for creating threads. If you were not writing a COCOA application, this is the best choice for creating threads. The POSIX interface is relatively simple-to-use and offers-ample flexibility for configuring your threads. For more information, see "Using POSIX Threads" |
|
Multiprocessing Services |
Multiprocessing Services is a legacy c-based interface used by applications transitioning from older versions of Mac OS. This technology are available in OS X and should are avoided for any new development. Instead, you should use the NSThread class or POSIX threads. If you need more information on this technology, see multiprocessing Services Programming Guide.
|
Although Apple supports 3 kinds of technologies, the most common one in our practice is the first, which uses Nsthread to control threads. There are 2 ways to initialize Nsthread, one is the traditional Init method (InitWithTarget:selector:object:), and the other is the class method (+ Detachnewthreadselector:totarget: Withobject:)
The Initwithtarget method uses the following:
Object : Image_url]; [Thread start];
The @selector inside is the entry point of the thread, which accepts only one incoming parameter and does not have a return value. Another need to emphasize is that Init simply creates the thread, and the Start method needs to be called to run the thread.
If you want to replace the Detachnewthreadselector class method, use the following:
[Nsthread detachnewthreadselector: @selector (downloadimage:) totarget:self Withobject:image_url];
We can see that the parameters are similar, but the threads run immediately and do not need to call the Start method manually.
We still look at the code, this code is very simple, is to implement the background download, and on a uiimage display.
Using the Nsthread method, the Initwithtarget approach is used:
#import"ViewController.h"#defineImage_url @ "http://williamzhang-public.qiniudn.com/dsc_0069.jpg "@interface Viewcontroller () @end @implementation viewcontroller@synthesize ImageView;- (void) Downloadimage: (nsstring*) url{NSAutoreleasePool*pool =[[NSAutoreleasePool alloc] init]; NSData*data =[[NSData alloc] Initwithcontentsofurl:[nsurl Urlwithstring:url]]; UIImage*image =[[UIImage alloc] initwithdata:data]; if(image) {[Self Performselectoronmainthread: @selector (updateUI:) withobject:image Waituntildone:yes]; } [Pool drain];}- (void) UpdateUI: (uiimage*) image{Self.imageView.image=image;}- (void) viewdidload{[Super Viewdidload]; Nsthread*thread = [[Nsthread alloc] initwithtarget:self selector: @selector (downloadimage:)Object: Image_url]; [Thread start]; }- (void) didreceivememorywarning{[Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}- (void) Dealloc {[ImageView release]; [Super Dealloc];} @end
In Viewdidload, we generate a thread and run it, the entry point of the thread is the Downloadimage: method, which is used to download the image and notifies the main thread to refresh the interface.
If you use the Detachnewthreadselector method, you can change the Viewdidload method slightly:
-(void) viewdidload{ [Super Viewdidload]; [Nsthread detachnewthreadselector: @selector (downloadimage:) totarget:self withobject:image_url];}
In development we always need to remember that the main thread is not able to run a long time unresponsive task, the example above is a very typical multithreaded application, but Apple still provides a more simple way to help us simplify the development of the trouble. This is the Performselectorinbackground:withobject: methods and PerformSelectorOnMainThread:withObject:waitUntilDone provided by the NSObject class: Method. In the above example, the PerformSelectorOnMainThread:withObject:waitUntilDone: method is used, which is used to make the main thread refresh the UI after downloading the image.
-(void) viewdidload{ [Super Viewdidload]; [Self Performselectorinbackground: @selector (downloadimage:) Withobject:image_url];}
If this is written, although it is also the essence or multi-threading, but you do not feel the existence of nsthread, you can simply understand the task of running a background, in many cases or more simple and clear some.
The last one is the generated thread, the memory management is also responsible for its own, so need to generate autorelease pool, if you turn on the ARC function, Then you can simply use the @autoreleasepool keyword.