簡介
大家好!我是Tony,一個熱愛技術,希望運用技術改變生活的的追夢男孩。閑話不多說,今天聊聊iOS的線程保活。主要內(nèi)容如下:
- 線程保活的運用
- 線程保活的方法
- 保活的線程如何回收
線程保活運用
在實際開發(fā)中經(jīng)常會遇到一些耗時,且需要頻繁處理的工作,這部分工作與UI無關,比如說大文件的下載,后臺間隔一段時間進行數(shù)據(jù)的上報,APM中開啟一個watch dog線程等。
線程保活的方法
我們都知道運用啟動后,后開啟一個主線程,這個線程一直監(jiān)聽這各種事件源,這個監(jiān)聽器就是RunLoop.對于RunLoop的原理分析,大家可以閱讀我的另一篇文章,這里就不做具體的描述。
自定義線程
這個我創(chuàng)建了一個TYThread,內(nèi)容如下:
#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(@"%@----子線程任務結(jié)束",[NSThread currentThread]);
}
}
run方法執(zhí)行完畢后,TYThread的dealloc方法也執(zhí)行了,說明一般情況下開啟線程任務后,當任務執(zhí)行完畢后,線程就會被銷毀,如果想讓線程不死掉的話,需要為線程添加一個RunLoop,具體代碼如下:
- (void)run {
@autoreleasepool {
for (int i = 0; i < 100; i++) {
NSLog(@"----子線程任務 %ld",(long)i);
}
NSLog(@"%@----子線程任務結(jié)束",[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]);
}
這樣就能釋放掉線程