zoukankan      html  css  js  c++  java
  • 浅谈一下对runloop的理解

    一 什么是RunLoop?

    从字面意思看就是运行循环,其实内部就是do-while循环,这个循环内部不断地处理各种任务(比 如Source,Timer,Observer)

    一个线程对应一个RunLoop,主线程的RunLoop默认已经启动,子线程的RunLoop得手动启动(run方法)

    RunLoop只能选择一个Mode启动,如果当前Mode中没有任何Source,Timer,Observer,那么就直接退出RunLoop 

    二 你在开发过程中怎么使用RunLoop?什么应用场景?

    开启一个常驻线程(让一个子线程不进入消亡状态,等待其他线程发来的消息,处理其他事件)

    在子线程中开启一个定时器

    在子线程中进行一些长期监控

    可以控制定时器在特定模式下运行

    可以让某些事件(行为,任务)在特定模式下执行

    可以添加observer监听RunLoop的状态,比如监听点击事件的处理(比如在所有点击事件前做一些处理)

    //下面用代码来介绍runloop在线程中的运用

    我们的目的是开辟一个子线程处理一些事件,然后将另一件事也放到第一个线程中处理

    #import "ViewController.h"

    @interface ViewController ()

    @property(nonatomic,strong)NSThread*thread;

    @end

     @implementation ViewController

    - (void)viewDidLoad {

        [super viewDidLoad];

           UIButton*but1=[UIButton buttonWithType:UIButtonTypeSystem];

        [but1 setFrame:CGRectMake(50, 50, 40, 30)];

        [but1 setBackgroundColor:[UIColor blueColor]];

        [but1 addTarget:self action:@selector(text1) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:but1];

       

        UIButton*but12=[UIButton buttonWithType:UIButtonTypeSystem];

        [but12 setFrame:CGRectMake(120, 50, 30, 30)];

        [but12 setBackgroundColor:[UIColor redColor]];

        [but12 addTarget:self action:@selector(text12) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:but12];

    }

    -(void)text1

    {

        NSLog(@"%@",[NSThread currentThread]);

        self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(text2) object:nil];

        [self.thread start];

      

    }

    -(void)text2

    {

        //获得当前子线程对应的runloop,也就是说一个进程的开始就会开启一个runloop对象,对于子线程的runloop对象是需要手动开启的,需要调用run方法;

        NSRunLoop*runloop=[NSRunLoop currentRunLoop];

         NSLog(@"2%@",[NSThread currentThread]);

        //runloop的作用是为了保证线程的存活,这里的 NSTimer是保证runloop不退出,若没有runloop的运行模式的话,这个runloop就会自动退出,

        

    //    //1第一种方法加定时器

    //    NSTimer*timer=[NSTimer timerWithTimeInterval:2 target:self selector:@selector(too1 ) userInfo:nil repeats:YES];

    //    [runloop addTimer:timer forMode:NSDefaultRunLoopMode];

    //    

        

       //2.给runloop加一个端口,保证runloop不退出

        [runloop addPort:[NSPort port] forMode:NSDefaultRunLoopMode];

          [runloop run];

    }

    //点击第二个红色按钮后继续让第一个线程继续工作

    -(void)text12

    {

        [self performSelector:@selector(text13) onThread:self.thread withObject:nil waitUntilDone:YES];//为了将其他的事件也在上一个线程中来执行,那么我们对上一个线程做了一个存活的处理,所以利用runloop来保证线程的存活,不让其退出;

    }

    -(void)text13

    {

     NSLog(@"13%@",[NSThread currentThread]);

    }

  • 相关阅读:
    《C++必知必会》读书笔记
    看美图是一种享受
    C指针-指向另一指针的指针
    顺序队列基本操作
    Using Windows Live Writer to write first offline blog
    堆和栈的区别 [摘录]
    进程与线程的区别
    《Effective C#》
    析构函数virtual与非virtual区别
    常用SQL语句技法
  • 原文地址:https://www.cnblogs.com/wpw19920808/p/5760407.html
Copyright © 2011-2022 走看看