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

首頁 > 系統 > iOS > 正文

iOS NSThread和NSOperation的基本使用詳解

2020-07-26 02:32:02
字體:
來源:轉載
供稿:網友

NSThread適合簡單的耗時任務的執行,它有兩種執行方法

- (void)oneClick{ [NSThread detachNewThreadSelector:@selector(doSomething:) toTarget:self withObject:@"oneClick"];}-(void)doSomething:(NSString*) str{ NSLog(@"%@",str);}- (void)twoClick{ NSThread* myThread = [[NSThread alloc] initWithTarget:self             selector:@selector(doSomething:)             object:@"twoClick"]; [myThread start];}

NSOperation適合需要復雜的線程調度的方法,然后它默認是使用主線程不會創建子線程

- (void)threeClick{ // 1.創建NSInvocationOperation對象 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; // 2.調用start方法開始執行操作 [op start];}- (void)run{ NSLog(@"------%@", [NSThread currentThread]);}- (void)fourClick{ NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{  // 在主線程  NSLog(@"1------%@", [NSThread currentThread]); }]; // 添加額外的任務(在子線程執行) [op addExecutionBlock:^{  NSLog(@"2------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{  NSLog(@"3------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{  NSLog(@"4------%@", [NSThread currentThread]); }]; [op start];}

以上這篇iOS NSThread和NSOperation的基本使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 博乐市| 寻乌县| 霍州市| 手机| 淮北市| 东山县| 保山市| 万年县| 石泉县| 平安县| 岳西县| 新闻| 若尔盖县| 伊宁市| 慈利县| 唐山市| 安多县| 延寿县| 称多县| 苏尼特右旗| 固始县| 安新县| 郑州市| 甘孜县| 天祝| 灌云县| 惠州市| 鄱阳县| 石家庄市| 南昌县| 寻乌县| 张家川| 雅江县| 常宁市| 浦东新区| 达孜县| 沅江市| 滕州市| 侯马市| 柯坪县| 富蕴县|