Multithreading for iOS development-thread status and multithreading for ios development
Multithreading for iOS development-thread status
1. Brief Introduction
Thread creation:
Self. thread = [[NSThread alloc] initWithTarget: self selector: @ selector (test) object: nil];
Note: There are multiple ways to create a thread. I will not introduce it too much here.
Thread enabling:
[Self. thread start];
Thread running and blocking:
(1) set thread blocking 1, blocking 2 seconds
[NSThread sleepForTimeInterval: 2.0];
(2) set thread blocking 2, with the current time as the benchmark to block for 4 seconds
NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 4.0];
[NSThread sleepUntilDate: date];
The performance of the thread in the memory when processing the blocking state: (the thread is removed from the schedulable thread pool and cannot be scheduled at this time)
Thread death:
When the job of a thread ends, an exception occurs, or a forced exit may cause the thread to die.
After the thread dies, the thread object is removed from the memory.
Ii. Sample Code
Sample Code 1:
1 // 2 // YYViewController. m 3 // 04-NSThread02-thread status 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 @ property (nonatomic, strong) NSThread * thread; 13 14 @ end15 16 @ implementation YYViewController17 18-(void) viewDidLoad19 {20 [super viewDidLoad]; 21 22 // create thread 23 self. thread = [[NSThread alloc] initWithTarget: self selector: @ selector (test) object: nil]; 24 // set the thread name 25 [self. thread setName: @ "thread A"]; 26} 27 // when the finger is pressed, enable thread 28-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event29 {30 // enable thread 31 [self. thread start]; 32} 33 34-(void) test35 {36 // get thread 37 NSThread * current = [NSThread currentThread]; 38 NSLog (@ "test --- print thread --- % @", self. thread. name); 39 NSLog (@ "test --- thread start --- % @", current. name); 40 41 // sets thread blocking 1, blocking 2 seconds 42 NSLog (@ "Next, thread blocking 2 seconds"); 43 [NSThread sleepForTimeInterval: 2.0]; 44 45 // The second method sets thread blocking 2, which is based on the current time to block 4 s 46 NSLog (@ "Next, thread blocking for 4 s "); 47 NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 4.0]; 48 [NSThread sleepUntilDate: date]; 49 for (int I = 0; I <20; I ++) {50 NSLog (@ "thread -- % d -- % @", I, current. name); 51 52} 53 NSLog (@ "test --- thread end --- % @", current. name); 54} 55 56 @ end
Print and view:
Sample Code 2 (exit the thread ):
1 // 2 // YYViewController. m 3 // 04-NSThread02-thread status 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014 itcase. all rights reserved. 7 // 8 9 # import "YYViewController. h "10 11 @ interface YYViewController () 12 @ property (nonatomic, strong) NSThread * thread; 13 14 @ end15 16 @ implementation YYViewController17 18-(void) viewDidLoad19 {20 [super viewDidLoad]; 21 22 // create thread 23 self. thread = [[NSThread alloc] initWithTarget: self selector: @ selector (test) object: nil]; 24 // set the thread name 25 [self. thread setName: @ "thread A"]; 26} 27 // when the finger is pressed, enable thread 28-(void) touchesBegan :( NSSet *) touches withEvent :( UIEvent *) event29 {30 // enable thread 31 [self. thread start]; 32} 33 34-(void) test35 {36 // get thread 37 NSThread * current = [NSThread currentThread]; 38 NSLog (@ "test --- print thread --- % @", self. thread. name); 39 NSLog (@ "test --- thread start --- % @", current. name); 40 41 // sets thread blocking 1, blocking 2 seconds 42 NSLog (@ "Next, thread blocking 2 seconds"); 43 [NSThread sleepForTimeInterval: 2.0]; 44 45 // The second method sets thread blocking 2, which is based on the current time to block 4 s 46 NSLog (@ "Next, thread blocking for 4 s "); 47 NSDate * date = [NSDate dateWithTimeIntervalSinceNow: 4.0]; 48 [NSThread sleepUntilDate: date]; 49 for (int I = 0; I <20; I ++) {50 NSLog (@ "thread -- % d -- % @", I, current. name); 51 if (5 = I) {52 // end thread 53 [NSThread exit]; 54} 55 56} 57 NSLog (@ "test --- thread end --- % @", current. name); 58} 59 60 @ end
Print example:
Note: When a thread is dead, it cannot be re-enabled. If you click the screen again to re-enable the thread after the thread is dead, the program will fail.