您好,登錄后才能下訂單哦!
NSThread適合簡(jiǎn)單的耗時(shí)任務(wù)的執(zhí)行,它有兩種執(zhí)行方法
- (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適合需要復(fù)雜的線程調(diào)度的方法,然后它默認(rèn)是使用主線程不會(huì)創(chuàng)建子線程
- (void)threeClick{ // 1.創(chuàng)建NSInvocationOperation對(duì)象 NSInvocationOperation *op = [[NSInvocationOperation alloc] initWithTarget:self selector:@selector(run) object:nil]; // 2.調(diào)用start方法開始執(zhí)行操作 [op start]; } - (void)run { NSLog(@"------%@", [NSThread currentThread]); } - (void)fourClick{ NSBlockOperation *op = [NSBlockOperation blockOperationWithBlock:^{ // 在主線程 NSLog(@"1------%@", [NSThread currentThread]); }]; // 添加額外的任務(wù)(在子線程執(zhí)行) [op addExecutionBlock:^{ NSLog(@"2------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{ NSLog(@"3------%@", [NSThread currentThread]); }]; [op addExecutionBlock:^{ NSLog(@"4------%@", [NSThread currentThread]); }]; [op start]; }
以上這篇iOS NSThread和NSOperation的基本使用詳解就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。