IOS多线程编程NSThread的使用方法

IOS多线程编程NSThread的使用方法

创新互联公司自2013年创立以来,是专业互联网技术服务公司,拥有项目成都做网站、网站设计网站策划,项目实施与项目整合能力。我们以让每一个梦想脱颖而出为使命,1280元郧西做网站,已为上家服务,为郧西各地企业和个人服务,联系电话:13518219792

NSThread是多线程的一种,有两种方法创建子线程

(1)优点:NSThread 比GCD、NSOperation都轻量级

(2)缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销

第一种是隐藏创建,有以下几种方式:

(1)多用于串行:- (id)performSelector:(SEL)aSelector withObject:(id)object;
(2)后台执行,多用于并行:- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg;
(3)延迟执行:- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
(4)回到主线程执行:- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(nullable id)arg waitUntilDone:(BOOL)wait;
注意:
(1)通过方法" + (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget selector:(SEL)aSelector object:(nullable id)anArgument; ",或"+ (void)cancelPreviousPerformRequestsWithTarget:(id)aTarget"停止执行; 

示例:

//创建子线程-隐式方法

// 子线程-串行 
[self performSelector:@selector(showCount:) withObject:@(11)]; 
[self performSelector:@selector(showCount:) withObject:@(12)]; 
[self performSelector:@selector(showCount:) withObject:@(23)]; 

// 子线程-并行(后台)  
[self performSelectorInBackground:@selector(showCount:) withObject:@(41)]; 
[self performSelectorInBackground:@selector(showCount:) withObject:@(42)]; 
// 回到主线程 
[self performSelectorOnMainThread:@selector(showCount:) withObject:@(51) waitUntilDone:YES]; 

// 子线程延迟执行 
[self performSelector:@selector(showCount:) withObject:@(61) afterDelay:5.0]; 
// 停止 
[NSObject cancelPreviousPerformRequestsWithTarget:self]; 

 第二种是显示创建,方式如下:

 - (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(nullable id)argument; 

注意:

 (1)通过方法" - (void)start; "开始执行;
 (2)通过方法" - (void)cancel; "停止执行;  

 示例:

 //创建子线程-显示方法

self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(showCount:) object:@(61)]; 
self.thread.name = @"计数"; 
[self.thread start]; 
[self.thread cancel]; 

代码示例

- (void)showCount:(NSNumber *)number 
{ 
 NSInteger count = arc4random() % 1000; 
 count = 1000; 
 for (int i = 0; i < count; i++) 
 { 
  NSLog(@"第 %@ 个 i = %@", number, @(i)); 
   
  // 休眠n秒再执行 
  [NSThread sleepForTimeInterval:0.2]; 
   
  // 停止 
//  BOOL isStop = [self.thread isCancelled]; 
//  if (isStop) 
//  { 
//   NSLog(@"2 停止"); 
//   break; 
//  } 
  if (isCancelThread) 
  { 
   NSLog(@"2 停止"); 
   break; 
  } 
 } 
} 
bool isCancelThread = NO; 
- (void)stopClick 
{ 
 [NSObject cancelPreviousPerformRequestsWithTarget:self]; 
  
 if (self.thread) 
 { 
  BOOL isExecuting = [self.thread isExecuting]; 
  if (isExecuting) 
  { 
   NSLog(@"1 停止"); 
//   [self.thread cancel]; 
   isCancelThread = YES; 
  } 
 } 
} 
- (void)downloadImage:(NSString *)imageUrl 
{ 
 NSURL *url = [NSURL URLWithString:imageUrl]; 
 NSData *data = [[NSData alloc] initWithContentsOfURL:url]; 
 UIImage *image = [[UIImage alloc] initWithData:data]; 
 if (image == nil) 
 { 
   
 } 
 else 
 { 
//  [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:YES]; 
  [self performSelectorInBackground:@selector(updateImage:) withObject:image]; 
 } 
  
// NSURL *url = [NSURL URLWithString:imageUrl]; 
// NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
// NSURLSession *session = [NSURLSession sharedSession]; 
// NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request completionHandler: ^(NSData *data, NSURLResponse *response, NSError *error) { 
//   
//  // 输出返回的状态码,请求成功的话为200 
//  NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 
//  NSInteger responseStatusCode = [httpResponse statusCode]; 
//  NSLog(@"%ld", responseStatusCode); 
//   
//  UIImage *image = [UIImage imageWithData:data]; 
////  [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:YES]; 
//  [self performSelectorInBackground:@selector(updateImage:) withObject:image]; 
// }]; 
//  
// // 使用resume方法启动任务 
// [dataTask resume]; 
} 
- (void)updateImage:(UIImage *)image 
{ 
 self.imageview.image = image; 
  
// self.imageview = [[UIImageView alloc] initWithFrame:CGRectMake(10.0, 10.0, (CGRectGetWidth(self.view.bounds) - 10.0 * 2), (CGRectGetWidth(self.view.bounds) - 10.0 * 2))]; 
// [self.view addSubview:self.imageview]; 
// self.imageview.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.2]; 
//  
// self.imageview.image = image; 
} 
NSString *imageUrl = @"http://ww1.sinaimg.cn/crop.0.0.1242.1242.1024/763fb12bjw8empveq3eq8j20yi0yiwhw.jpg"; 
// 隐藏创建 
// [self performSelectorInBackground:@selector(downloadImage:) withObject:imageUrl]; 
[self performSelectorOnMainThread:@selector(downloadImage:) withObject:imageUrl waitUntilDone:YES]; 
// 创建子线程-显示方法 
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:imageUrl]; 
self.thread.name = @"imageDownload"; 
[self.thread start]; 

IOS多线程编程NSThread的使用方法

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!


当前文章:IOS多线程编程NSThread的使用方法
链接地址:http://pwwzsj.com/article/jdcchi.html