zoukankan      html  css  js  c++  java
  • 定时器在多线程中的使用

    在多线程中使用定时器必须开启Runloop,因为只有开启Runloop保持现成为活动状态,才能保持定时器不断执行

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        
        [self performSelectorInBackground:@selector(testMultiThread) withObject:nil];
    }
    - (void) testMultiThread {
        NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
        
        // 1 - 用这种方式创建的定时器,会被自动加入到Runloop中
        [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];    
        
        // 2 -用这种方式创建的定时器,不会被自动加入到Runloop中,需要手动加入
    // 2-1 -创建定时器
    //NSTimer * timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeAction) userInfo:nil repeats:YES];
    // 2-2 -将定时器加入Runloop中
      //[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; //开启Runloop来使线程保持存活状态 [[NSRunLoop currentRunLoop] run]; [pool release]; NSLog(@"线程结束"); } - (void) timeAction { NSLog(@"%s",__FUNCTION__); }
  • 相关阅读:
    1.shell编程-变量的高级用法
    1.python简介
    1.numpy的用法
    1.MySQL(一)
    1.HTML
    1.Go-copy函数、sort排序、双向链表、list操作和双向循环链表
    1.Flask URL和视图
    1.Django自学课堂
    1.Django安装与运行
    ajax跨站请求伪造
  • 原文地址:https://www.cnblogs.com/benbenzhu/p/3761018.html
Copyright © 2011-2022 走看看