美文网首页
关于线程编程

关于线程编程

作者: samingzhong | 来源:发表于2016-04-07 23:54 被阅读12次

注:此文基本参考 苹果官方文档


与runloop的关系

此为文档首层目录:


QQ20160407-0.png

可以看到Run Loops在此独占一章。。就说明了runloop跟线程的关系不菲。
展开关于线程编程这块:


QQ20160407-1.png
其中线程支持这块,runloop也在里边。可以看到在介绍runloop之前有这么一段:

Because threads are relatively expensive to create in terms of memory and time, it is therefore recommended that your entry point function do a significant amount of work or set up a run loop to allow for recurring work to be performed.

线程的创建相当消耗内存跟时间,因此建议线程里执行的任务最好是重要的。或者我们可以建立一个runloop来执行循环类型的任务。
也就是:别随便创建线程因为挺耗资源的;会被反复执行的活,可以通过建立runloop来做。

To configure a run loop, all you have to do is launch your thread, get a reference to the run loop object, install your event handlers, and tell the run loop to run. The infrastructure provided by OS X handles the configuration of the main thread’s run loop for you automatically. If you plan to create long-lived secondary threads, however, you must configure the run loop for those threads yourself.

要想配置一个runloop,你需要做的就是:

 1. 启动线程
 2. 获得runloop对象的引用
 3. 设置事件的处理
 4. 启动runloop

另外,主线程的runloop是由系统自动设置好的。所以其他线程要想成为常驻性线程,需要自己亲自动手配置好对应的runloop。
接下来是关于runloop的正式介绍。

runloop

Run loops are part of the fundamental infrastructure associated with threads. A run loop is an event processing loop that you use to schedule work and coordinate the receipt of incoming events. The purpose of a run loop is to keep your thread busy when there is work to do and put your thread to sleep when there is none.

runloop是与线程息息相关的一个基础部分。runloop说白了就是一个用来干活的事件处理环。目的是为了让线程在有事的时候忙起来,没事的时候休眠。

Your application does not need to create these objects explicitly; each thread, including the application’s main thread, has an associated run loop object.

你不必为专门的创建一个runloop,每一个线程,包括主线程,都有一个相关联的runloop对象。

Only secondary threads need to run their run loop explicitly, however. The app frameworks automatically set up and run the run loop on the main thread as part of the application startup process.

你自定义的线程如果需要用到runloop,那就需要亲自启动它。然而,app框架在主线程上自动设置并启动了runloop。

下面将会讲述什么是runloop以及如何配置runloop:

runloop的结构

正如其名,runloop就是一个环,我们的线程进去之后,针对不同事件执行相应的处理过程。使用runloop对象的run方法开启这一切。

runloop接收两种类型的事件源: Input Sources和Timer Sources。
Input Sources传递异步事件,通常来源于其他线程或者其他进程。Timer Sources则传递同步事件,这个事件是调度的或者重复的。请看下图:


runloop内部结构以及 源runloop内部结构以及 源

未完待续...

相关文章

  • 关于线程编程

    注:此文基本参考 苹果官方文档 与runloop的关系 此为文档首层目录: 可以看到Run Loops在此独占一章...

  • 线程编程指南----关于线程的编程

    注:本文翻译自About Threaded Programming 关于线程的编程 多年来,几乎所有的电脑性能都被...

  • 多线程编程基础之 wait()、notify()、sleep()

    前言:在面试过程中 关于多线程编程这一块是经常问到的 为了更好的理解关于多线程编程基础特地的记录此文章把思路理清楚...

  • 完结

    关于多线程并发编程,先写到这里,后面可能继续插入更新。

  • Java多线程编程简明教程

    Java多线程编程简明教程 缘起 关于多线程编程的教程汗牛充栋了,比如阿里集团内部就有一粟、高铁等大牛的讲座,更不...

  • iOS 多线程编程(关于多线程编程)

    多年来,计算机的最大性能主要受限于它的中心微处理器的速度。然而由于个别处理器已经开始达到它的瓶颈限制,芯片制造商开...

  • iOS POSIX多线程编程

    关于多线程的介绍、多线程的创建、使用场景和Runloop可以参考《iOS多线程编程指南》。已上传到GitHub仓库...

  • 多线程编程

    多线程编程之Linux环境下的多线程(一)多线程编程之Linux环境下的多线程(二)多线程编程之Linux环境下的...

  • 多线程开发

    关于多线程编程首先我们要了解清楚什么是线程,什么是进程,以及他们之间的区别和联系。 所谓的线程就是CPU调度(执行...

  • 多线程编程

    关于多线程编程首先我们要了解清楚什么是线程,什么是进程,以及他们之间的区别和联系。 所谓的线程就是CPU调度(执行...

网友评论

      本文标题:关于线程编程

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