IOS学习 多线程NSThread 消息循环(了解)涉及定时器
来源:互联网 发布:linux telnet包 编辑:程序博客网 时间:2024/05/15 01:36
#import "ViewController.h"
@interface ViewController ()
{
BOOL shouldKeepRunning;
}
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UITextView *textView = [[UITextViewalloc]initWithFrame:CGRectMake(0,20,300,150)];
textView.text =@"“北京房地产中介下架了近万套高价房源。”昨日,一位地产人士表示,这在业内已经不是一个秘密。日前,市住建委开展了对房地产中介专项执法检查,包括擅自发布房源信息、哄抬房价等不良行为。此前,北京各家房地产中介纷纷下架了高价房源,北京甚至出现大批“学区房”一夜之间价格统一跌到14.99万元的现象。";
textView.font = [UIFontsystemFontOfSize:20];
[self.viewaddSubview:textView];
shouldKeepRunning =NO;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
[selfdemo2];
}
//主线程消息循环,默认开启
- (void)demo{
//创建timer 参数4:自定义 参数5:是否重复执行
NSTimer *timer = [NSTimertimerWithTimeInterval:2target:selfselector:@selector(task)userInfo:nilrepeats:YES];
//把定时器加入当前线程的消息循环中 参数1:定时源 参数2:模式
/*现象:
*NSDefaultRunLoopMode:拖动界面,定时源不运行
*NSRunLoopCommonModes:拖动界面,正常运行,没有影响
没有拖动界面:kCFRunLoopDefaultMode
拖动界面:UITrackingRunLoopMode
*/
[[NSRunLoopcurrentRunLoop]addTimer:timerforMode:NSRunLoopCommonModes];
}
- (void)task{
//输出当前循环模式
NSLog(@"%@",[NSRunLoopcurrentRunLoop].currentMode);
NSLog(@"task is running");
}
//子线程消息循环,默认关闭
- (void)demo2{
NSThread *thread = [[NSThreadalloc]initWithTarget:selfselector:@selector(task2)object:nil];
[thread start];
/*
//在指定线程中的消息循环中加入源
//参数1:方法/参数2:指定线程/参数3:对象/参数4:等待方法执行完成
[self performSelector:@selector(addtask) onThread:thread withObject:nil waitUntilDone:NO];
//开启消息循环 使用run方法,无法停止
[[NSRunLoop currentRunLoop]run];
//第二种:指定循环运行时间
[[NSRunLoop currentRunLoop]runUntilDate:[NSDate dateWithTimeIntervalSinceNow:2]];
*/
//第三种:apple推荐的方法 (了解)
// BOOL shouldKeepRunning = YES; // global
NSRunLoop *theRL = [NSRunLoopcurrentRunLoop];
while (shouldKeepRunning && [theRLrunMode:NSDefaultRunLoopModebeforeDate:[NSDatedistantFuture]]);
NSLog(@"over");
}
-(void)task2{
//输出当前循环模式
// NSLog(@"%@",[NSRunLoop currentRunLoop].currentMode);
NSLog(@"task is running:%@",[NSThreadcurrentThread]);
}
-(void)addtask{
NSLog(@"addtask is running");
}
- IOS学习 多线程NSThread 消息循环(了解)涉及定时器
- iOS多线程学习---NSThread
- iOS 多线程(一)NSThread
- ios多线程(NSThread)(swift)
- iOS 多线程(二)NSThread
- iOS多线程学习(2)——NSThread和NSOperation
- iOS 学习笔记 --- 多线程基础(2)--- NSThread
- iOS多线程学习笔记(GCD、RunLoop、NSThread)
- iOS学习笔记-多线程基础(NSThread)
- IOS多线程系统学习之NSThread
- iOS多线程学习-NSThread、Cocoa NSOperation、GCD
- iOS-多线程编程学习之NSThread(三)
- [iOS学习]关于多线程和NSThread
- ios学习 多线程NSThread 基本属性
- IOS学习 多线程NSThread 共享变量 卖票
- iOS多线程系列(1)NSThread
- iOS开发--多线程编程(一)NSThread
- iOS多线程编程(二)------ NSThread
- 关于mysql的update、delete、和insert into能否使用别名问题
- JVM Notes
- 小菜鸟的全栈之路
- HDU 5115:Dire Wolf 区间dp
- android 数据库sqlite的使用
- IOS学习 多线程NSThread 消息循环(了解)涉及定时器
- Android异步消息处理机制完全解析,带你从源码的角度彻底理解
- Android线程初窥(一)
- STM32_Boot0,Boot1引脚设置
- ora2pg
- docker部署
- Android studio编译NDK
- RabbitMQ基础概念详细介绍
- 浏览器及时感知服务端数据变化的方式