Network start --- multithreading --- NSThread-01-Basic use (understanding) (2), ------ nsthread-01-
1 # import "HMViewController. h "2 3 @ interface HMViewController () 4 5 @ end 6 7 @ implementation HMViewController 8 9-(void) viewDidLoad10 {11 [super viewDidLoad]; 12 // Do any additional setup after loading the view, typically from a nib.13} 14 15 // download operation, 16-(void) download :( NSString *) url17 {18 NSLog (@ "download things --- % @", url, [NSThread currentThread]); 19 20 21 22 23 24} 25 26-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event27 {28 [self createThread3]; 29} 30 31/** 32 * thread creation method 333 implicitly create thread and automatically start thread 34 */35-(void) createThread336 {37 // these two do not create threads, execute 38 // [self defined mselector: @ selector (download :) withObject: @ "http://c.gif"] in the current thread; 39 // [self download: @ "http://c.gif"]; 40 41 // This implicit creation thread automatically creates a thread in the background and executes the download method, 42 [self implements mselectorinbackground: @ selector (download :) withObject: @ "http://c.gif"]; 43} 44 45/** 46 * thread creation method 247 after a thread is created, the thread is automatically started 48 */49-(void) createThread250 {51 // separate a new thread from the current thread 52 [NSThread detachNewThreadSelector: @ selector (download :) toTarget: self withObject: @ "http://a.jpg"]; 53} 54 55/** 56 * thread creation method 157 the thread will not be started automatically after being created, you need to write a statement to start thread 58. This method is the best of the three. You can set the thread to 59 60 */61-(void) in detail) createThread162 {63 // creation thread 64 NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (download :) object: @ "http:// B .png"]; 65 66 // thread name. When printing a thread, you can see the name of the printed thread 67 thread. name = @ "Download thread"; 68 69 object: // The parameter to be passed by the method you open up the thread to execute. For example, upload a url here, in the upload to the download method 70 // this parameter is 71 72 to be uploaded from the current thread to the Child thread // start the thread (call the download Method of self) only the startup thread can call the download method 73 [thread start] of self; // This statement must be used to start thread 74 // and the execution process is executed in the Child thread, is a newly opened thread to put time-consuming operations into the Child thread to 75 76 [NSThread mainThread]; // obtain the main thread 77 NSThread * current = [NSThread currentThread]; // obtain the current thread 78 79 80 [thread isMainThread]; // checks whether the current thread (New thread) is the main thread and returns the Bool value 81 [NSThread isMainThread]; // determine whether the method for executing the code is in the main thread, and return the Bool value 82 83} 84 85 @ end