当前位置: 首页 > news >正文

途牛网站建设的基本特点鼓楼网页seo搜索引擎优化

途牛网站建设的基本特点,鼓楼网页seo搜索引擎优化,建站平台选择建议,wordpress浮动播放器文章目录 没有线程常驻会怎么样#xff1f; 线程常驻线程保活 没有线程常驻会怎么样#xff1f; 我们一般写一个子线程#xff0c;子线程执行完分配的任务后就会自动销毁#xff0c;比如下面这个情况#xff1a; 我们先重写一下NSThread里面的dealloc方法#xff0c;打印… 文章目录 没有线程常驻会怎么样 线程常驻线程保活 没有线程常驻会怎么样 我们一般写一个子线程子线程执行完分配的任务后就会自动销毁比如下面这个情况 我们先重写一下NSThread里面的dealloc方法打印什么时候会调用dealloc方法。 #import NSThreadNewDealloc.himplementation NSThread (NewDealloc) - (void)dealloc {NSLog(%s, __func__); } end在ViewController里调用方法 implementation FirstViewController- (void)viewDidLoad {[super viewDidLoad];NSThread *thread [[NSThread alloc] initWithTarget:self selector:selector(doSomeThing) object:nil];[thread start]; } - (void)doSomeThing {NSLog(%s, __func__); }根据打印结果我们可以看到在子线程执行完任务后线程自动销毁。 而我们有时会需要经常在一个子线程中执行任务频繁的创建和销毁线程就会造成资源浪费这时候就要用到RunLoop来使线程长时间存活了 线程常驻 开发应用程序的过程中如果后台操作十分频繁比如后台播放音乐、下载文件等等我们希望这条线程永远常驻内存 我们可以添加一条用于常驻内存的强引用子线程在该线程的RunLoop下添加一个Sources开启RunLoop interface FirstViewController () property (nonatomic, strong) NSThread *thread; endself.thread [[NSThread alloc] initWithTarget:self selector:selector(run1) object:nil];[self.thread start];- (void)run1 {NSLog(----run1-----);/*如果不加这句会发现runloop创建出来就挂了因为runloop如果没有CFRunLoopSourceRef事件源输入或者定时器就会立马消亡。下面的方法给runloop添加一个NSport就是添加一个事件源也可以添加一个定时器或者observer让runloop不会挂掉*/[[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];// 方法1 ,23实现的效果相同让runloop无限期运行下去// 方法2 // [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];// 方法3 // [[NSRunLoop currentRunLoop] runUntilDate:[NSDate distantFuture]];[[NSRunLoop currentRunLoop] run];// 测试是否开启了RunLoop如果开启RunLoop则来不了这里因为RunLoop开启了循环。NSLog(未开启RunLoop); }- (void)touchesBegan:(NSSetUITouch * *)touches withEvent:(UIEvent *)event {// 利用performSelector在self.thread的线程中调用run2方法执行任务[self performSelector:selector(run2) onThread:self.thread withObject:nil waitUntilDone:NO]; }- (void)run2 {NSLog(----run2------); } 无论点击屏幕多少次都不会dealloc线程。 我们必须保证线程不消亡才可以在后台接受时间处理所以如果没有实现添加NSPort或者NSTimer会发现执行完run方法线程就会消亡后续再执行touchbegan方法无效。 实现了上面三个方法之一就可以发现执行完了run方法这个时候再点击屏幕可以不断执行test方法因为线程self.thread一直常驻后台等待事件加入其中然后执行。 线程保活 RunLoop的启动和关闭方法在上一篇博客的最后讲过【iOS】—— RunLoop初学 我们直奔主题 通过以上关于RunLoop启动和关闭的方法分析我们大概有这样一个思路 我们想要控制RunLoop就需要使用runMode:beforeDate:方法因为其他两种方法一个无法停止一个只能依赖超时机制CFRunLoopStop() 方法只会结束当前的一次的runMode:beforeDate:方法调用我们必须再做点什么 针对以上疑问有以下解答 首先因为runMode:beforeDate:方法是单次调用我们需要给它加上一个循环否则调用一次就over了和不使用RunLoop的效果大同小异这个循环的条件可以默认设置为YES当调用stop方法时执行CFRunLoopStop() 方法并且将循环条件改为NO就可以使循环停止RunLoop退出 #import SecondViewController.hinterface SecondViewController () property (nonatomic, strong) NSThread *aThread; property (nonatomic, assign) BOOL stopped; endimplementation SecondViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor [UIColor whiteColor];// 添加一个停止RunLoop的按钮UIButton *stopButton [UIButton buttonWithType:UIButtonTypeRoundedRect];[self.view addSubview:stopButton];stopButton.frame CGRectMake(180, 180, 100, 50);stopButton.titleLabel.font [UIFont systemFontOfSize:20];[stopButton setTitle:stop forState:UIControlStateNormal];stopButton.tintColor [UIColor blueColor];[stopButton addTarget:self action:selector(stop) forControlEvents:UIControlEventTouchUpInside];// 用于返回的按钮UIButton *backButton [UIButton buttonWithType:UIButtonTypeRoundedRect];[self.view addSubview:backButton];backButton.frame CGRectMake(180, 380, 100, 50);backButton.titleLabel.font [UIFont systemFontOfSize:20];[backButton setTitle:back forState:UIControlStateNormal];backButton.tintColor [UIColor orangeColor];[backButton addTarget:self action:selector(back) forControlEvents:UIControlEventTouchUpInside];self.stopped NO;__weak typeof(self) weakSelf self;self.aThread [[NSThread alloc] initWithBlock:^{NSLog(go);[[NSRunLoop currentRunLoop] addPort:[[NSPort alloc] init] forMode:NSDefaultRunLoopMode];while (!weakSelf.stopped) {[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];}NSLog(ok);}];[self.aThread start]; }- (void)touchesBegan:(NSSetUITouch * *)touches withEvent:(UIEvent *)event {[self performSelector:selector(doSomething) onThread:self.aThread withObject:nil waitUntilDone:NO]; }// 子线程需要执行的任务 - (void)doSomething {NSLog(%s %, __func__, [NSThread currentThread]); }- (void)stop {// 在子线程调用stopif (self.aThread) {// 在子线程调用stop[self performSelector:selector(stopThread) onThread:self.aThread withObject:nil waitUntilDone:YES];} }// 用于停止子线程的RunLoop - (void)stopThread {// 设置标记为NOself.stopped YES;// 停止RunLoopCFRunLoopStop(CFRunLoopGetCurrent());NSLog(%s %, __func__, [NSThread currentThread]);self.aThread nil; }- (void)dealloc {NSLog(%s, __func__); }- (void)back {[self stop];[self dismissViewControllerAnimated:YES completion:nil]; } end需要注意的一点是如果我们的ViewController已经被销毁了线程并没有死这就造成了内存泄漏了。所以我们要注意在ViewController被dealloc之前先stop线程。
http://www.dnsts.com.cn/news/12796.html

相关文章:

  • 站酷网官网下载怎么注册公司流程和费用
  • 垂直电商网站有哪些seo整站优化技术培训
  • 东莞英文建站公司公司注册地址可以是住宅
  • flash网站的优势网站服务器哪家好些
  • 网站做百度推广需要什么材料网站标题关键优化
  • 怎么在传奇网站上做宣传庆阳网站建设报价
  • 网站开发学生职业规划浙江省建设工程检测协会网站
  • 招远做网站公司百度权重1是什么意思
  • 网络营销网站建设的角度wordpress面包屑导航代码
  • 猪八戒网仿照哪个网站做的软件开发合同模板下载
  • 网站三网合一案例尚德机构
  • 网站在百度上搜不到了广州微信营销公司
  • 怎么给网站做备份呢拼多多网店怎么注册开店
  • iis7如何搭建网站ftp网站地图怎么做
  • 个人网站备案 费用自己买空间让网络公司做网站好吗
  • 网站的连接二维码怎么做广东如何做网站设计
  • c网站建设seo计费
  • 电子商务网站关键技术小程序源码教程
  • 网站建设与管理技术发展微信怎么建小网站
  • 网站排版设计欣赏网站推广方法有
  • 整站优化seo排名点击绍兴h5建站
  • 自己怎样制作公司网站青岛网站设计定制
  • 濮阳微信网站建设html怎么做网页动态背景
  • html做网站经验技巧wordpress 注册图片
  • 无锡网站制作那些苏州城乡建设局网站
  • 郑州哪家公司做网站好网页设计与编程
  • 网站建设介绍ppt模板赣州经济
  • 网站设计建设服务烟台网站推广效果好
  • 九龙坡网站建设哪家好专业网站快速排名优势
  • 北京网站建设策划解决方案个人简历word可编辑