国产探花免费观看_亚洲丰满少妇自慰呻吟_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的基本使用詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 郯城县| 谢通门县| 贵定县| 盐山县| 芜湖县| 林芝县| 淮安市| 德阳市| 镇坪县| 西藏| 临安市| 德州市| 莆田市| 贵州省| 喜德县| 铜梁县| 当阳市| 铜山县| 高雄市| 元谋县| 田东县| 蛟河市| 师宗县| 怀仁县| 文登市| 三都| 罗田县| 鄯善县| 吕梁市| 什邡市| 红河县| 府谷县| 阆中市| 洛南县| 宝鸡市| 囊谦县| 永登县| 德安县| 繁峙县| 凤翔县| 永嘉县|