欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

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

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

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

(2)缺點:需要自己管理線程的生命周期,線程同步。線程同步對數(shù)據(jù)的加鎖會有一定的系統(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 = @"計數(shù)"; 
[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方法啟動任務(wù) 
// [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ū)交流討論,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

相關(guān)文章

  • 詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實現(xiàn)

    詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實現(xiàn)

    這篇文章主要介紹了詳解iOS開發(fā)中UItableview控件的數(shù)據(jù)刷新功能的實現(xiàn),代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下
    2015-12-12
  • iOS提取APP中的圖片資源的方法

    iOS提取APP中的圖片資源的方法

    這篇文章主要介紹了iOS提取APP中的圖片資源的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-11-11
  • iOS中字符串換行的實現(xiàn)方法

    iOS中字符串換行的實現(xiàn)方法

    大家應(yīng)該都有所體會,單行字符數(shù)過多會影響美觀,所以下面這篇文章主要給大家介紹了關(guān)于iOS中字符串換行的實現(xiàn)方法,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2018-07-07
  • Objective-C優(yōu)雅使用KVO觀察屬性值變化

    Objective-C優(yōu)雅使用KVO觀察屬性值變化

    這篇文章主要為大家介紹了Objective-C優(yōu)雅使用KVO觀察屬性值變化示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • CodeIgniter輔助函數(shù)helper詳解

    CodeIgniter輔助函數(shù)helper詳解

    這篇文章主要介紹了CodeIgniter輔助函數(shù)helper,需要的朋友可以參考下
    2014-07-07
  • IOS中NSPredicate和NSRegularExpression校驗正則表達式區(qū)別

    IOS中NSPredicate和NSRegularExpression校驗正則表達式區(qū)別

    本文文章通過實例代碼給大家講述了在IOS開發(fā)中NSPredicate和NSRegularExpression校驗正則表達式區(qū)別,需要的朋友趕快學(xué)習(xí)下吧。
    2018-01-01
  • iOS中GCD定時器詳解

    iOS中GCD定時器詳解

    ?CADisplayLink、NSTimer是基于RunLoop機制的,如果RunLoop的任務(wù)過于繁重,有可能會導(dǎo)致前兩個定時器不準時,這篇文章主要介紹了iOS中GCD定時器的相關(guān)知識,需要的朋友可以參考下
    2013-02-02
  • Objective-C 宏定義詳細介紹

    Objective-C 宏定義詳細介紹

    這篇文章主要介紹了Objective-C 宏定義詳細介紹的相關(guān)資料,這樣開發(fā)起來,更有效率,更好,更簡潔,需要的朋友可以參考下
    2016-10-10
  • iOS開發(fā)之枚舉用法小結(jié)

    iOS開發(fā)之枚舉用法小結(jié)

    大家都知道枚舉是C語言中的一種基本數(shù)據(jù)類型,是一個"被命名的整型常量"的集合,它不參與內(nèi)存的占用和釋放,我們在開發(fā)中使用枚舉的目的只有一個,那就是為了增加代碼的可讀性。下面就來來看看在iOS中枚舉的用法,有需要的朋友們可以看看。
    2016-09-09
  • 談?wù)刋Code9的新變化

    談?wù)刋Code9的新變化

    這篇文章主要介紹了談?wù)刋Code9的新變化,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-09-09

最新評論