簡介
大家好!我是Tony,一個熱愛技術恤左,希望運用技術改變生活的的追夢男孩。閑話不多說搀绣,今天聊聊iOS的線程狈纱活。主要內容如下:
- 線程绷椿迹活的運用
- 線程鼻裳迹活的方法
- 保活的線程如何回收
線程甭槟恚活運用
在實際開發(fā)中經(jīng)常會遇到一些耗時纲仍,且需要頻繁處理的工作,這部分工作與UI無關贸毕,比如說大文件的下載郑叠,后臺間隔一段時間進行數(shù)據(jù)的上報,APM中開啟一個watch dog線程等明棍。
線程毕绺铮活的方法
我們都知道運用啟動后,后開啟一個主線程摊腋,這個線程一直監(jiān)聽這各種事件源沸版,這個監(jiān)聽器就是RunLoop.對于RunLoop的原理分析,大家可以閱讀我的另一篇文章兴蒸,這里就不做具體的描述视粮。
自定義線程
這個我創(chuàng)建了一個TYThread,內容如下:
#import "TYThread.h"
@implementation TYThread
- (void)dealloc {
NSLog(@"%s",__func__);
}
@end
僅重寫了dealloc方法,下面是具體的測試代碼
MJThread *thread = [[MJThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
//run方法
- (void)run {
@autoreleasepool {
for (int i = 0; i < 100; i++) {
NSLog(@"----子線程任務 %ld",(long)i);
}
NSLog(@"%@----子線程任務結束",[NSThread currentThread]);
}
}
run方法執(zhí)行完畢后橙凳,TYThread的dealloc方法也執(zhí)行了蕾殴,說明一般情況下開啟線程任務后,當任務執(zhí)行完畢后痕惋,線程就會被銷毀区宇,如果想讓線程不死掉的話,需要為線程添加一個RunLoop,具體代碼如下:
- (void)run {
@autoreleasepool {
for (int i = 0; i < 100; i++) {
NSLog(@"----子線程任務 %ld",(long)i);
}
NSLog(@"%@----子線程任務結束",[NSThread currentThread]);
NSRunLoop *runLoop = [NSRunLoop currentRunLoop];
// 往RunLoop里面添加Source\Timer\Observer值戳,Port相關的是Source1事件
//添加了一個Source1议谷,但是這個Source1也沒啥事,所以線程在這里就休眠了堕虹,不會往下走卧晓,----end----一直不會打印
[runLoop addPort:[NSMachPort port] forMode:NSRunLoopCommonModes];
[runLoop run];
NSLog(@"%s ----end----", __func__);
}
}
通過打印發(fā)現(xiàn)芬首,線程的dealloc方法不會執(zhí)行,NSLog(@"%s ----end----", __func__);
也不會執(zhí)行逼裆。下面通過performSelector方法郁稍,往線程中添加任務
- (IBAction)start:(id)sender {
[self performSelector:@selector(doSomethingInSubThread) onThread:self.thread withObject:nil waitUntilDone:NO];
//waitUntilDone:YES 等到子線程任務執(zhí)行完再執(zhí)行下面NSLog
//NO 不用等到子線程執(zhí)行完再執(zhí)行下面NSLog(下面NSLog在主線程,test在子線程胜宇,同時執(zhí)行)
NSLog(@"123");
}
任務可以正常執(zhí)行耀怜,說明線程一直是活著的。
蓖┯洌活的線程如何回收
添加stop的執(zhí)行方法如下:
- (IBAction)stop:(id)sender {
[self performSelector:@selector(quitRunLoop) onThread:self.thread withObject:nil waitUntilDone:NO];
}
解決循環(huán)引用問題
//如果使用如下方式創(chuàng)建thread财破,self會引用thread,thread會引用self从诲,會造成循環(huán)引用左痢。
TYThread *thread = [[TYThread alloc] initWithTarget:self selector:@selector(run) object:nil];
//需要在quitRunLoop中,進行如下設置
- (void)quitRunLoop {
// 設置標記為NO
self.stopped = YES;
// 停止RunLoop
CFRunLoopStop(CFRunLoopGetCurrent());
[self.thread cancel];
//解決循環(huán)引用問題
self.thread = nil;
NSLog(@"%s %@", __func__, [NSThread currentThread]);
}
這樣就能釋放掉線程