国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

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

2019-10-21 18:44:19
字體:
來源:轉載
供稿:網友

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

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,多線程NSThread,NSThread的使用方法

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


注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 婺源县| 沙坪坝区| 宜都市| 日喀则市| 深州市| 宝应县| 扎赉特旗| 青岛市| 山东省| 温宿县| 揭东县| 策勒县| 南投市| 枣强县| 门源| 西充县| 清涧县| 应用必备| 城口县| 增城市| 武陟县| 固始县| 丰台区| 郑州市| 长治县| 辽中县| 儋州市| 共和县| 佳木斯市| 师宗县| 萍乡市| 大化| 松江区| 瑞昌市| 页游| 叶城县| 邵武市| 天祝| 崇明县| 永丰县| 台山市|