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

  

<强> IOS多线程编程NSThread的使用方法

  

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

  

(1)优点:NSThread比肾小球囊性肾病,NSOperation都轻量级
  

  

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

  

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

  

(1)多用于串行:- (id) performSelector: (SEL) aSelector withObject: (id)对象;
  (2)后台执行,多用于并行:- (void) performSelectorInBackground: (SEL) aSelector withObject: arg (nullable id),
  (3)延迟执行:- (void) performSelector: (SEL) aSelector withObject:(可空id)条件下afterDelay: (NSTimeInterval)延迟;
  (4)回到主线程执行:- (void) performSelectorOnMainThread: (SEL) aSelector withObject:(可空id) arg waitUntilDone: (BOOL)等;
  注意:
  (1)通过方法”+ (void) cancelPreviousPerformRequestsWithTarget: (id) aTarget选择器:(SEL) aSelector对象:(可空id)条件下,,”,或“+ (void) cancelPreviousPerformRequestsWithTarget: (id) aTarget“停止执行;,
  

  

示例:   

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

     //子线程——串行   【自我performSelector: @ selector (showCount:) withObject: @ (11)];   【自我performSelector: @ selector (showCount:) withObject: @ (12)];   【自我performSelector: @ selector (showCount:) withObject: @ (23)];   
        //子线程——并行(后台),   【自我performSelectorInBackground: @ selector (showCount:) withObject: @ (41)];   【自我performSelectorInBackground: @ selector (showCount:) withObject: @ (42)];   之前         //回到主线程   【自我performSelectorOnMainThread: @ selector (showCount:) withObject: @ (51) waitUntilDone:是的);   
        //子线程延迟执行   【自我performSelector: @ selector (showCount:) withObject: @ (61) afterDelay: 5.0);   之前         //停止   (NSObject cancelPreviousPerformRequestsWithTarget:自我);之前      

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

        - (instancetype) initWithTarget: (id)目标选择器:(SEL)选择器对象:(可空id)参数;   之前      

注意:
  

  

,(1)通过方法”- (void)开始;“开始执行;
  ,(2)通过方法”- (void)取消;“停止执行;,,
  

  

,示例:
  

  

,//创建子线程——显示方法
  

        自我。线程=[[NSThread alloc] initWithTarget:自我选择器:@ selector (showCount:)对象:@ (61);   self.thread.name=@”计数”;   (自我。线程开始);   (自我。线程取消);   之前      

代码示例
  

        - (void) showCount: (NSNumber *)号码   {   NSInteger数=arc4random () % 1000;   数=1000;   for (int i=0;我& lt;计数;我+ +)   {   NSLog(@“第% @个i=% @”,数字,@(我));//休眠n秒再执行   [NSThread sleepForTimeInterval: 0.2);//停止//BOOL isStop=[自我。线程isCancelled];//如果(isStop)//{//NSLog(@“2停止”);//中断;//}   如果(isCancelThread)   {   NSLog(@“2停止”);   打破;   }   }   }   之前            bool isCancelThread=没有;   - (void) stopClick   {   (NSObject cancelPreviousPerformRequestsWithTarget:自我);      如果(self.thread)   {   BOOL isExecuting=[自我。线程isExecuting];   如果(isExecuting)   {   NSLog(@“1停止”);//(自我。线程取消);   isCancelThread=是的;   }   }   }   之前            - (void) downloadImage: imageUrl (NSString *)   {   NSURL * url=[NSURL URLWithString imageUrl):;   NSData *数据=[[NSData alloc]并:url];   用户界面图像*图像=[[界面图像alloc] initWithData:数据);   如果(图片==nil)   {      }   其他的   {   (自我performSelectorOnMainThread://@ selector (updateImage:) withObject:图像waitUntilDone:是的);   【自我performSelectorInBackground: @ selector (updateImage:) withObject:图像);   }//NSURL * url=[NSURL URLWithString imageUrl):;//NSURLRequest *请求=[NSURLRequest requestWithURL: url];//NSURLSession *会话=[NSURLSession sharedSession);//NSURLSessionDataTask * dataTask=[会话dataTaskWithRequest:请求completionHandler: ^ (NSData *数据,NSURLResponse *反应,NSError *误差){//////输出返回的状态码,请求成功的话为200//NSHTTPURLResponse * httpResponse=(NSHTTPURLResponse *)反应;//NSInteger responseStatusCode=[httpResponse statusCode];//NSLog (@“% ld”, responseStatusCode);////用户界面图像*图像=[界面图像imageWithData:数据);////(自我performSelectorOnMainThread: @ selector (updateImage:) withObject:图像waitUntilDone:是的);   (自我performSelectorInBackground://@ selector (updateImage:) withObject:图像);//});//////使用的简历方法启动任务//(dataTask简历);   }   

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