IOS RunLoop 之我解
来源:互联网 发布:怎么看淘宝店铺的地址 编辑:程序博客网 时间:2024/05/16 15:36
//on mainThread
-(void)testRunLoop
{
NSLog(@"start new thread!!!");
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
[selfperformSelector:@selector(delayRuning)withObject:nilafterDelay:0.0];
[pool release];
}
-(void)delayRuning
{
NSLog(@"new thread delay running!!!");
}
代码的执行结果会是怎样?
//on mainThread
-(void)testRunLoop
{
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
[selfperformSelector:@selector(delayRuning)withObject:nilafterDelay:0.0];
[pool release];
}
-(void)delayRuning
{
NSLog(@"new thread delay running!!!");
}
这段程序执行的结果会是怎样?
//on mainThread
-(void)testRunLoop
{
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
[selfperformSelector:@selector(delayRuning)withObject:nilafterDelay:0.0];
[[NSRunLoopcurrentRunLoop] run];
//或者:
[pool release];
}
-(void)delayRuning
{
NSLog(@"new thread delay running!!!");
}
//on mainThread
-(void)testRunLoop
{
_stopRunning = NO;
_startInterval = [NSDatetimeIntervalSinceReferenceDate];
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
while (!_stopRunning)
{
NSLog(@"the loop begin time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
BOOL result = [[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDate dateWithTimeIntervalSinceNow:10]];
NSLog(@"the loop end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
NSLog(@"end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
NSLog(@"newThreadRuning begin time: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
_stopRunning = YES;
NSLog(@"newThreadRuning end time: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
[pool release];
}
这段代码的运行结果会是怎样?上面的循环会不会一直不停的跑?
start new thread!!!the loop begin time is: 0.012369 s
newThreadRuning begin time: 0.012445 s
newThreadRuning end time: 0.013623 s
the loop end time is: 10.013487 s
end time is: 10.013777 s
为什么在子线程改变_stopRunning变量之后主线程的循环没有立即退出,而循环没有不停的跑。//on mainThread
-(void)testRunLoop
{
_stopRunning = NO;
_startInterval = [NSDatetimeIntervalSinceReferenceDate];
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
while (!_stopRunning)
{
NSLog(@"the loop begin time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
BOOL result = [[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedateWithTimeIntervalSinceNow:10]];
NSLog(@"the loop end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
NSLog(@"end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
NSLog(@"newThreadRuning begin time: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
[selfperformSelectorOnMainThread:@selector(setRuning)withObject:nilwaitUntilDone:NO];
NSLog(@"newThreadRuning end time: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
[pool release];
-(void)setRuning
{
_stopRunning = YES;
}
上面这个代码的执行结果又会是怎么样?
start new thread!!!
the loop begin time is: 0.000947 s
newThreadRuning begin time: 0.001023 s
newThreadRuning end time: 0.001267 s
the loop end time is: 0.001327 s
end time is: 0.001542 s
这时当子线程改变之后主线程立即就退出了,原因就是performSelectorOnMainThread:方法给主线程发送了事件,因此主线程启动了run loop唤醒了线程。这个地方如果你做的是ui相关的test程序的话,在test:4-4的时候你如果随意点击一下ui,也会引发循环的退出,因为点击事件同样触发了主线程的run loop。方法performSelectorOnMainThread:中的waitUntilDone:的作用是否等待@selector方法执行的完成。//on mainThread
-(void)testRunLoop
{
_stopRunning = NO;
_startInterval = [NSDatetimeIntervalSinceReferenceDate];
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
NSLog(@"end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
-(void)runOnNewThread
{
NSAutoreleasePool * pool = [[NSAutoreleasePoolalloc] init];
while (!_stopRunning)
{
[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];
NSLog(@"runing");
}
NSLog(@"newThreadRuning end ");
[poolrelease];}
这段代码的执行结果会是怎么样?
NSLog(@"runing");这段代码能够得到执行吗?//on mainThread
-(void)testRunLoop
{
_stopRunning = NO;
_startInterval = [NSDatetimeIntervalSinceReferenceDate];
NSLog(@"start new thread!!!");
[NSThreaddetachNewThreadSelector:@selector(runOnNewThread)toTarget:selfwithObject:nil];
NSLog(@"end time is: %f s",[NSDatetimeIntervalSinceReferenceDate] - _startInterval);
}
-(void)runOnNewThread
{
NSAutoreleasePool *pool = [[NSAutoreleasePoolalloc] init];
[NSTimerscheduledTimerWithTimeInterval:10target:selfselector:@selector(repeatRunning)userInfo:nilrepeats:YES];
while (!_stopRunning){
[[NSRunLoopcurrentRunLoop] runMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]];
NSLog(@"runing");
}
NSLog(@"newThreadRuning end");
[pool release];
}
-(void)repeatRunning
{
NSLog(@"new thread repeat Running!!!");
}
这段代码的执行结果又会是什么样的?
这段代码中:
NSLog(@"runing");NSURL *url=[[NSURLalloc] initWithString:@"http:www.qq.com"];
NSMutableURLRequest *request=[[NSMutableURLRequestalloc] initWithURL:url];
NSURLConnection *connect=[[NSURLConnectionalloc] initWithRequest:requestdelegate:selfstartImmediately:YES];
NSPort *port=[NSPortport];
NSRunLoop *runloop=[NSRunLoopcurrentRunLoop];
[runloop addPort:port forMode:NSDefaultRunLoopMode];
[connect scheduleInRunLoop:runloop forMode:NSDefaultRunLoopMode];
[connect start];
[runloop run];
- IOS RunLoop 之我解
- IOS之RunLoop
- iOS开发之Runloop
- iOS学习之 - Runloop
- 关于iOS之Runloop
- iOS开发之runloop
- IOS之初见runLoop
- iOS之RunLoop
- iOS开发之Runloop
- ios开发之Runloop简介
- iOS SDK详解之Runloop
- iOS开发之RunLoop-赵鹏举
- 关于IOS 我对Runloop 的理解
- iOS 之 Thread Runloop浅显认识说起
- 深入理解iOS开发之RunLoop
- iOS学习之RunLoop的深入理解
- iOS之runloop篇(面试可用)
- iOS基础面试题之RunLoop篇
- 通讯录电话号码的跳转获取
- 资金池屡禁不绝 银监会督促整改4月大限
- ubuntu安装samba实现与windows共享文件
- WIN32_LEAN_AND_MEAN 的作用
- 厚积薄发,丰富的公用类库积累,助你高效进行系统开发(14)
- IOS RunLoop 之我解
- Java应用程序窗口关闭的方法
- php学习之路
- Faltt让你支持你喜欢的创作者
- VDI 虚拟桌面基础架构(VDI,Virtual Desktop Infrastructure)
- Click Modular Router
- POJ1936《All in All》
- AJAX技术
- 在Android上实现图像颜色过滤与反转