zoukankan      html  css  js  c++  java
  • IOS RunLoop 常驻线程的实现

    线程常驻,正如其名,我们要实现的事让一个线程长期存在,不被销毁。

    这时会有人说,那还不简单吗。

    但是这里我们要实现的事如何让线程座椅待命,而且并不是主线程。

    首先介绍一下正常情况下的线程使用。

    复制代码
    //
    //  ViewController.m
    //  CX RunLoop 常驻线程的实现
    //
    //  Created by ma c on 16/3/30.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXThread.h"
    @interface ViewController ()
    
    @property (nonatomic, strong)CXThread * thread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         NSThread* thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        [thread start];
    
    }
    -(void)run{
        
        NSLog(@"run -- 旭宝爱吃鱼");
        
    }
    -(void)test{
        
        NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]);
        
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        [self test];
        
        //让test方法在线程thread上实现
    //    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];
        
    }
    @end
    复制代码

    上面的代码知识简单的实现了线程的使用。

    下面是其效果图(注意线程的销毁)

    实际上test与thread并没有关系。

    我知识简单的让其输出默认的主线程日志,以供后面对比。

    下面是让thread为全局变量

    复制代码
    //
    //  ViewController.m
    //  CX RunLoop 常驻线程的实现
    //
    //  Created by ma c on 16/3/30.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXThread.h"
    @interface ViewController ()
    
    @property (nonatomic, strong)CXThread * thread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        [_thread start];
    
    }
    -(void)run{
        
        NSLog(@"run -- 旭宝爱吃鱼");
        
    }
    -(void)test{
        
        NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]);
        
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
        [self test];
        
        //让test方法在线程thread上实现
    //    [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:nil];
        
    }
    @end
    复制代码

    由效果图我们可以发现。thread并没有销毁。而且test,依旧是在主线程上实现的。

    但我们想要的是test在thread上实现(实际开发中是不允许耗时操作在主线程中的)

    我们让test在thread中实现:(注意虾米那方法并不成功)

    复制代码
    //
    //  ViewController.m
    //  CX RunLoop 常驻线程的实现
    //
    //  Created by ma c on 16/3/30.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXThread.h"
    @interface ViewController ()
    
    @property (nonatomic, strong)CXThread * thread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        [_thread start];
    
    }
    -(void)run{
        
        NSLog(@"run -- 旭宝爱吃鱼");
        
    }
    -(void)test{
        
        NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]);
        
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    
    //    让test方法在线程thread上实现
        [self performSelector:@selector(test) onThread:_thread withObject:nil waitUntilDone:YES];
        
    }
    @end
    复制代码

    为什么会不成功呢??(我真的点击了)

    原因是我们只是单纯的建立了一个线程。。。很单纯的。。。考虑一下我们该怎么做。

    那么我们有两种做法实现。

    方法一(比较正常的方法)

    复制代码
    //
    //  ViewController.m
    //  CX RunLoop 常驻线程的实现
    //
    //  Created by ma c on 16/3/30.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXThread.h"
    @interface ViewController ()
    
    @property (nonatomic, strong)CXThread * thread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        [_thread start];
    
    }
    -(void)run{
        
        NSLog(@"run -- 旭宝爱吃鱼");
        //添加Port 实时监听
        [[NSRunLoop currentRunLoop] addPort:[NSPort port] forMode:NSDefaultRunLoopMode];
        //添加runloop
        [[NSRunLoop currentRunLoop]run];
        
        
    }
    -(void)test{
        
        NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]);
        
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    
    //    让test方法在线程thread上实现
        [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
        
    }
    @end
    复制代码

    就是这么简单。

    方法二

    复制代码
    //
    //  ViewController.m
    //  CX RunLoop 常驻线程的实现
    //
    //  Created by ma c on 16/3/30.
    //  Copyright © 2016年 xubaoaichiyu. All rights reserved.
    //
    
    #import "ViewController.h"
    #import "CXThread.h"
    @interface ViewController ()
    
    @property (nonatomic, strong)CXThread * thread;
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
    
         _thread = [[CXThread alloc]initWithTarget:self selector:@selector(run) object:nil];
        
        [_thread start];
    
    }
    -(void)run{
        
        NSLog(@"run -- 旭宝爱吃鱼");
        
        while (1) {
            //添加runloop
            [[NSRunLoop currentRunLoop]run];
        }
    }
    -(void)test{
        
        NSLog(@"test -- 旭宝爱吃鱼 %@",[NSThread currentThread]);
        
    }
    -(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
        
    
    //    让test方法在线程thread上实现
        [self performSelector:@selector(test) onThread:self.thread withObject:nil waitUntilDone:NO];
        
    }
    @end
    复制代码

  • 相关阅读:
    hdu 1595(最短路变形好题)
    hdu 5253(最小生成树)
    hdu 2363(枚举+最短路好题)
    hdu 3440(差分约束好题)
    poj 3169&hdu3592(差分约束)
    hdu 3339(最短路+01背包)
    hdu 2145(迪杰斯特拉)
    CodeForces 581D Three Logos
    CodeForces 510E Fox And Dinner
    CodeForces 484D Kindergarten
  • 原文地址:https://www.cnblogs.com/wuyuxin/p/7045572.html
Copyright © 2011-2022 走看看