- Please write the final output of the following code
-
(void)viewDidLoad {
[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(functionTestA) userInfo:nil repeats:NO];
[self functionTestB];
[self functionTestC];
} -
(void)functionTestA { NSLog(@“functionTestA…”); }
-
(void)functionTestB {
NSLog(@“functionTestB…”);
[NSThread sleepForTimeInterval:10];
} -
(void)functionTestC { NSLog(@“functionTestC…”); }
执行顺序 BCA
时间 B - 0
C - 10
A - 10.01
NSTimer 加在runloop上,runloop可以理解为只要硬件在运行,runloop就会不断接收到硬件上的脉冲,当接收到5秒后,就会把A方法加入到当前队列的后面去,当前队列已有BC方法
所以执行是这样子的,一开始,当前队列是主队列,已有BC方法的执行语句,执行B方法,当前队列会挂起10秒钟,在第5秒时,A方法会被插入到当前队列的最后,在5秒后执行C方法,执行完C方法执行A方法