運行結果列印: 原文文檔介紹: Runs the loop once, blocking for input in the specified mode until a given date. //執行loop一次,堵塞等待給定模式的輸入直至給定的時間點 Parameters mode The mo ...
// // ViewController.m // ThreadTest // // Created by skyko on 16/5/31. // Copyright © 2016年 helios. All rights reserved. // #import "ViewController.h" @interface ViewController () @property (nonatomic, strong) NSThread *thread; @property (nonatomic, assign) BOOL isAlive; @property (nonatomic, strong) NSTimer *timer; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; NSLog(@"viewDidLoad"); self.isAlive = YES; self.thread = [[NSThread alloc] initWithTarget:self selector:@selector(workThread:) object:nil]; [self.thread setName:@"MyCustomThread"]; [self.thread start]; //延遲3s後向線程添加任務 dispatch_time_t time = dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC); dispatch_after(time, dispatch_get_main_queue(), ^{ [self performSelector:@selector(handleSomeThing) onThread:self.thread withObject:nil waitUntilDone:NO]; }); } - (void)workThread:(id)data { NSLog(@"workThread"); NSRunLoop *runloop = [NSRunLoop currentRunLoop]; [runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];//不添加這一行的話,在有任務添加前下麵的while迴圈會一直執行,原因見後面解釋 while (_isAlive) { //isAlive用於控制該線程的運行周期,當設置isAlive為NO時,該線程執行完當前任務後退出並銷毀 NSLog(@"--->"); //在設置的時間內執行NSDefaultRunLoopMode模式的任務 //如果隊列中暫時沒有任務會一直堵塞直至有任務進入隊列 //任務執行完成後接著迴圈 //超時會進入下一次迴圈 [runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; NSLog(@"<---"); } } - (void)handleSomeThing { NSLog(@"handleSomeThing before sleep"); [NSThread sleepForTimeInterval:1.0f]; NSLog(@"handleSomeThing after sleep"); } @end
運行結果列印:
2016-05-31 11:42:25.020 ThreadTest[1940:76011] viewDidLoad 2016-05-31 11:42:25.020 ThreadTest[1940:76221] workThread 2016-05-31 11:42:25.021 ThreadTest[1940:76221] ---> 2016-05-31 11:42:28.311 ThreadTest[1940:76221] handleSomeThing before sleep 2016-05-31 11:42:29.315 ThreadTest[1940:76221] handleSomeThing after sleep 2016-05-31 11:42:29.315 ThreadTest[1940:76221] <--- 2016-05-31 11:42:29.315 ThreadTest[1940:76221] --->
原文文檔介紹:
Runs the loop once, blocking for input in the specified mode until a given date.
//執行loop一次,堵塞等待給定模式的輸入直至給定的時間點
Parameters
mode |
The mode in which to run. You may specify custom modes or use one of the modes listed in Run Loop Modes. |
limitDate |
The date until which to block. |
Return Value
YES if the run loop ran and processed an input source or if the specified timeout value was reached; otherwise, NO if the run loop could not be started.
//當runloop正在運行並處理了一個輸入源或者超時返回YES,否則如果runloop並未啟動就返回NO
Discussion
If no input sources or timers are attached to the run loop, this method exits immediately and returns NO; otherwise, it returns after either the first input source is processed or limitDate is reached. Manually removing all known input sources and timers from the run loop does not guarantee that the run loop will exit immediately. OS X may install and remove additional input sources as needed to process requests targeted at the receiver’s thread. Those sources could therefore prevent the run loop from exiting.
如果沒有一個輸入源或者timer添加到runloop中,該方法會立刻退出並返回NO(這就是上面demo中添加下麵代碼的原因),否則該方法會在執行完隊列中第一個輸入源或超時後返回,手動移除runloop中所有輸入源和timer並不能保證runloop立刻退出.OS X系統可能線上程中添加或移除一些額外的輸入源來處理必須的任務,因此這些輸入源可能會阻止runloop的退出.
[runloop addPort:[NSMachPort port] forMode:NSDefaultRunLoopMode];
NOTE
A timer is not considered an input source and may fire multiple times while waiting for this method to return
timer不是一個輸入源並且可能在等待該方法返回過程中會多次啟動
總結:
[runloop runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
1.在設置的時間內執行設置的模式的任務 2.如果隊列中暫時沒有任務會一直堵塞直至有任務進入隊列 3.任務執行完成後該方法退出 4.超時後該方法退出