最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

IOS多線程編程NSThread的使用方法

 更新時間:2017年10月11日 10:16:32   作者:番薯大佬  
這篇文章主要介紹了IOS多線程編程NSThread的使用方法的相關資料,希望通過本文能幫助到大家,讓大家理解使用多線程的方法,需要的朋友可以參考下

IOS多線程編程NSThread的使用方法

NSThread是多線程的一種,有兩種方法創(chuàng)建子線程

(1)優(yōu)點:NSThread 比GCD、NSOperation都輕量級

(2)缺點:需要自己管理線程的生命周期,線程同步。線程同步對數據的加鎖會有一定的系統(tǒng)開銷

第一種是隱藏創(chuàng)建,有以下幾種方式:

(1)多用于串行:- (id)performSelector:(SEL)aSelector withObject:(id)object;
(2)后臺執(zhí)行,多用于并行:- (void)performSelectorInBackground:(SEL)aSelector withObject:(nullable id)arg;
(3)延遲執(zhí)行:- (void)performSelector:(SEL)aSelector withObject:(nullable id)anArgument afterDelay:(NSTimeInterval)delay;
(4)回到主線程執(zhí)行:- (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"停止執(zhí)行; 

示例:

//創(chuàng)建子線程-隱式方法

// 子線程-串行 
[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]; 

// 子線程延遲執(zhí)行 
[self performSelector:@selector(showCount:) withObject:@(61) afterDelay:5.0]; 
// 停止 
[NSObject cancelPreviousPerformRequestsWithTarget:self]; 

 第二種是顯示創(chuàng)建,方式如下:

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

注意:

 (1)通過方法" - (void)start; "開始執(zhí)行;
 (2)通過方法" - (void)cancel; "停止執(zhí)行;  

 示例:

 //創(chuàng)建子線程-顯示方法

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秒再執(zhí)行 
  [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) { 
//   
//  // 輸出返回的狀態(tài)碼,請求成功的話為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"; 
// 隱藏創(chuàng)建 
// [self performSelectorInBackground:@selector(downloadImage:) withObject:imageUrl]; 
[self performSelectorOnMainThread:@selector(downloadImage:) withObject:imageUrl waitUntilDone:YES]; 
// 創(chuàng)建子線程-顯示方法 
self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(downloadImage:) object:imageUrl]; 
self.thread.name = @"imageDownload"; 
[self.thread start]; 

如有疑問請留言或者到本站社區(qū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關文章

  • iOS使用UIScorllView實現兩指縮放功能

    iOS使用UIScorllView實現兩指縮放功能

    兩指縮放功能不僅可以用UIPinchGestureRecognizer手勢來實現,還能用UIScorllView來實現,UIScrollView可以輕松的實現最大與最小縮放值,以及滾動的效果,效果非常棒,具體實例代碼大家參考下本文吧
    2017-03-03
  • iOS開發(fā)實現簡單抽屜效果

    iOS開發(fā)實現簡單抽屜效果

    這篇文章主要為大家詳細介紹了iOS開發(fā)實現簡單抽屜效果,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • iOS獲取圖片區(qū)域主色的方法

    iOS獲取圖片區(qū)域主色的方法

    這篇文章主要為大家詳細介紹了iOS獲取圖片區(qū)域主色的方法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • iOS開發(fā)學習TableView展現一個list實例

    iOS開發(fā)學習TableView展現一個list實例

    這篇文章主要為大家介紹了iOS系列學習TableView展現一個list實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-11-11
  • iOS利用CoreImage實現人臉識別詳解

    iOS利用CoreImage實現人臉識別詳解

    OS的人臉識別從iOS 5(2011)就有了,不過一直沒怎么被關注過。人臉識別API允許開發(fā)者不僅可以檢測人臉,也可以檢測到面部的一些特殊屬性,比如說微笑或眨眼。下面這篇文章主要給大家介紹了iOS利用CoreImage實現人臉識別的相關資料,需要的朋友可以參考下。
    2017-05-05
  • iOS中封裝.framework及使用的方法詳解

    iOS中封裝.framework及使用的方法詳解

    這篇文章主要給大家介紹了關于iOS中封裝.framework及使用的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧。
    2018-04-04
  • ios Plist文件配置方法

    ios Plist文件配置方法

    下面小編就為大家分享一篇ios Plist文件配置方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-02-02
  • iOS通過多種方式創(chuàng)建控制器

    iOS通過多種方式創(chuàng)建控制器

    這篇文章主要為大家詳細介紹了iOS通過多種方式創(chuàng)建控制器的相關方法,感興趣的小伙伴們可以參考一下
    2016-04-04
  • IOS開發(fā)之@property的詳細介紹

    IOS開發(fā)之@property的詳細介紹

    這篇文章主要介紹了IOS開發(fā)之@property的詳細介紹的相關資料,希望通過本文能幫助到大家,大家理解并會使用,需要的朋友可以參考下
    2017-09-09
  • IOS打開系統(tǒng)相機的閃光燈

    IOS打開系統(tǒng)相機的閃光燈

    今天給大家分享一下如何調用iphone的拍照功能和打開閃光燈,有些代碼我也不太理解,很多是在網上借鑒其他人的。
    2015-05-05

最新評論

湘潭县| 沙洋县| 萨嘎县| 内黄县| 青川县| 桐梓县| 肃北| 五大连池市| 桂东县| 乡城县| 黑山县| 喀喇沁旗| 绥化市| 云和县| 莒南县| 定日县| 罗甸县| 兴城市| 大连市| 阿拉善左旗| 海阳市| 紫云| 通河县| 阳江市| 开平市| 永顺县| 都昌县| 修文县| 阳新县| 中山市| 玛沁县| 南宁市| 玛纳斯县| 肃北| 宁国市| 安顺市| 舒兰市| 洪湖市| 毕节市| 肥乡县| 成武县|