美文网首页
常驻线程

常驻线程

作者: AlerStar | 来源:发表于2017-04-10 20:44 被阅读0次
    
    #import "ViewController.h"
    
    @interface ViewController ()
    
    /**
     *  常驻线程
     */
    @property(strong,nonatomic) NSThread * myThread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        
        [self creatMyThread];
        
    }
    
    #pragma mark - System Method
    
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        [self performSelector:@selector(runTwo) onThread:self.myThread withObject:nil waitUntilDone:NO];
    }
    
    - (void)runTwo
    {
        NSLog(@"Run_Two");
        NSLog(@"常驻线程任务执行中");
    }
    
    
    #pragma mark - Private Method
    
    /**
     *  创建并且开启常驻线程
     */
    - (void)creatMyThread
    {
        self.myThread = [[NSThread alloc]initWithTarget:self selector:@selector(runOne) object:nil];
        
        [self.myThread start];
    }
    
    - (void)runOne
    {
        // 获取RunLoop (获取时创建RunLoop)
        [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop] run];
        
        NSLog(@"RunLoop");
    }
    
    @end
    

    相关文章

      网友评论

          本文标题:常驻线程

          本文链接:https://www.haomeiwen.com/subject/vvseattx.html