问题描述
我是 iOS 开发的新手.现在我对线程"和队列"这两个概念感到很困惑.我所知道的是它们都是关于多线程编程的.谁能为我解释这两个概念以及它们之间的区别?提前致谢!
I am new to iOS development. Now I am quite confused about the two concepts: "thread" and "queue". All I know is that they both are about multithread programming. Can anyone interpret those two concepts and the difference between them for me? Thanks in advance!
推荐答案
在阅读我的答案之前,您可能需要考虑阅读此内容 - 从线程迁移
Before you read my answer you might want to consider reading this - Migrating away from Threads
我将讨论保持在理论上,因为您的问题没有任何代码示例.这两种结构都是提高应用程序响应能力和提高应用程序响应能力所必需的.可用性.
I am keeping the discussion theoretical as your question does not have any code samples. Both these constructs are required for increasing app responsiveness & usability.
消息队列是一种数据结构,用于保存从发送消息到接收者检索并对其采取行动的消息.通常,队列被用作连接"(数据)生产者和连接"生产者的一种方式.消费者(数据).
A message queue is a data structure for holding messages from the time they're sent until the time the receiver retrieves and acts on them. Generally queues are used as a way to 'connect' producers (of data) & consumers (of data).
线程池是执行某种处理的线程池.线程池通常会附加某种线程安全队列(参考消息队列),以允许您将要完成的作业排队.这里的队列通常被称为任务队列".
A thread pool is a pool of threads that do some sort of processing. A thread pool will normally have some sort of thread-safe queue (refer message queue) attached to allow you to queue up jobs to be done. Here the queue would usually be termed 'task-queue'.
因此,线程池可以存在于您的生产者端(生成数据)或消费者端(处理数据).而传递"这些数据的方法是通过队列.为什么需要这个中间人"——
So in a way thread pool could exist at your producer end (generating data) or consumer end (processing the data). And the way to 'pass' that data would be through queues. Why the need for this "middleman" -
- 它将系统解耦.生产者不了解消费者和反之亦然.
- 如果生产者数据出现峰值,消费者不会受到数据的轰炸.队列长度会增加,但消费者是安全的.
示例:
在 iOS 中,主线程(也称为 UI 线程)非常重要,因为它负责将事件分派给适当的小部件,这包括绘图事件,基本上是用户看到的 UI 和相互作用.
In iOS the main thread, also called the UI thread, is very important because it is in charge of dispatching the events to the appropriate widget and this includes the drawing events, basically the UI that the user sees & interacts.
如果您触摸屏幕上的按钮,UI 线程会将触摸事件分派给应用程序,然后应用程序设置其按下状态并向事件 queue 发送请求.UI 线程将请求出列并通知小部件重绘自身.
If you touch a button on screen, the UI thread dispatches the touch event to the app, which in turn sets its pressed state and posts an request to the event queue. The UI thread dequeues the request and notifies the widget to redraw itself.
这篇关于iOS开发中的“线程"和“队列"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!


大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)