我是 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开发中的“线程"和“队列"有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!