iOS开发中'thread'和'queue'有什么区别? [重复]

时间:2022-06-13 14:38:36

This question already has an answer here:

这个问题在这里已有答案:

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!

我是iOS开发的新手。现在我对这两个概念感到困惑:“线程”和“队列”。我所知道的是,它们都是关于多线程编程的。任何人都能解释这两个概念以及它们之间的区别吗?提前致谢!

2 个解决方案

#1


12  

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" -

因此,在某种程度上,线程池可能存在于生产者端(生成数据)或消费者端(处理数据)。而“传递”数据的方式是通过队列。为什么需要这个“中间人” -

  1. It decouples the systems. Producers do not know about consumers & vice versa.
  2. 它解耦系统。生产者不了解消费者,反之亦然。

  3. The Consumers are not bombarded with data if there is a spike in Producer data. The queue length would increase but the consumers are safe.
  4. 如果生产者数据出现峰值,消费者不会被数据轰炸。队列长度会增加,但消费者是安全的。

Example:

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.

在iOS中,主线程(也称为UI线程)非常重要,因为它负责将事件分派给适当的小部件,这包括绘图事件,基本上是用户看到和交互的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.

如果触摸屏幕上的按钮,UI线程会将触摸事件分派给应用程序,然后应用程序会设置其按下状态并将请求发布到事件队列。 UI线程使请求出列并通知窗口小部件重绘自身。

#2


18  

How NSOperationQueue and NSThread Works:

NSOperationQueue和NSThread如何工作:

NSThread:

  1. iOS developers have to write code for the work/process he want to perform along with for the creation and management of the threads themselves.
  2. iOS开发人员必须为他想要执行的工作/流程编写代码,以便创建和管理线程本身。

  3. iOS developers have to be careful about a plan of action for using threads.
  4. iOS开发人员必须小心使用线程的行动计划。

  5. iOS developer have to manage posiable problems like reuseability of thread, lockings etc. by them self.
  6. iOS开发人员必须自己管理可靠的问题,如线程的可重用性,锁定等。

  7. Thread will consume more memory too.
  8. 线程也会消耗更多内存。

NSOperationQueue:

  1. The NSOperation class is an abstract class which encapsulates the code and data associated with a single task.
  2. NSOperation类是一个抽象类,它封装了与单个任务相关的代码和数据。

  3. Developer needs to use subclass or one of the system-defined subclasses of NSOperation to perform the task.
  4. 开发人员需要使用子类或NSOperation的系统定义子类之一来执行任务。

  5. Add operations into NSOperationQueue to execute them.
  6. 将操作添加到NSOperationQueue中以执行它们。

  7. The NSOperationQueue creates a new thread for each operation and runs them in the order they are added.
  8. NSOperationQueue为每个操作创建一个新线程,并按添加顺序运行它们。

  9. Operation queues handle all of the thread management, ensuring that operations are executed as quickly and efficiently as possible.
  10. 操作队列处理所有线程管理,确保尽可能快速有效地执行操作。

  11. An operation queue executes operations either directly by running them on secondary threads or indirectly using GCD (Grand Central Dispatch).
  12. 操作队列直接通过在辅助线程上运行它们或间接使用GCD(Grand Central Dispatch)来执行操作。

  13. It takes care of all of the memory management and greatly simplifies the process.
  14. 它负责所有内存管理并大大简化了流程。

  15. If you don’t want to use an operation queue, you can also execute an operation by calling its start method. It may make your code too complex.
  16. 如果您不想使用操作队列,还可以通过调用其start方法来执行操作。它可能会使您的代码过于复杂。

How To Use NSThread And NSOperationQueue:

如何使用NSThread和NSOperationQueue:

NSThread:

  1. Though Operation queues is the preferred way to perform tasks concurrently, depending on application there may still be times when you need to create custom threads.
  2. 虽然操作队列是同时执行任务的首选方式,但根据应用程序的不同,您可能仍需要创建自定义线程。

  3. Threads are still a good way to implement code that must run in real time.
  4. 线程仍然是实现必须实时运行的代码的好方法。

  5. Use threads for specific tasks that cannot be implemented in any other way.
  6. 将线程用于无法以任何其他方式实现的特定任务。

  7. If you need more predictable behavior from code running in the background, threads may still offer a better alternative.
  8. 如果您需要从后台运行的代码中获得更多可预测的行为,则线程仍可提供更好的替代方案。

NSOperationQueue:

  1. Use NSOperationQueue when you have more complex operations you want to run concurrently.
  2. 如果要同时运行更复杂的操作,请使用NSOperationQueue。

  3. NSOperation allows for subclassing, dependencies, priorities, cancellation and a supports a number of other higher-level features.
  4. NSOperation允许子类化,依赖性,优先级,取消以及支持许多其他更高级别的功能。

  5. NSOperation actually uses GCD under the hood so it is as multi-core, multi-thread capable as GCD.
  6. NSOperation实际上在引擎盖下使用GCD,因此它像GCD一样具有多核,多线程。

Now you should aware about advantages and disadvantages of NSTread and NSOperation. You can use either of them as per needs of your application.

现在您应该了解NSTread和NSOperation的优缺点。您可以根据应用程序的需要使用其中任何一个。

#1


12  

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" -

因此,在某种程度上,线程池可能存在于生产者端(生成数据)或消费者端(处理数据)。而“传递”数据的方式是通过队列。为什么需要这个“中间人” -

  1. It decouples the systems. Producers do not know about consumers & vice versa.
  2. 它解耦系统。生产者不了解消费者,反之亦然。

  3. The Consumers are not bombarded with data if there is a spike in Producer data. The queue length would increase but the consumers are safe.
  4. 如果生产者数据出现峰值,消费者不会被数据轰炸。队列长度会增加,但消费者是安全的。

Example:

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.

在iOS中,主线程(也称为UI线程)非常重要,因为它负责将事件分派给适当的小部件,这包括绘图事件,基本上是用户看到和交互的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.

如果触摸屏幕上的按钮,UI线程会将触摸事件分派给应用程序,然后应用程序会设置其按下状态并将请求发布到事件队列。 UI线程使请求出列并通知窗口小部件重绘自身。

#2


18  

How NSOperationQueue and NSThread Works:

NSOperationQueue和NSThread如何工作:

NSThread:

  1. iOS developers have to write code for the work/process he want to perform along with for the creation and management of the threads themselves.
  2. iOS开发人员必须为他想要执行的工作/流程编写代码,以便创建和管理线程本身。

  3. iOS developers have to be careful about a plan of action for using threads.
  4. iOS开发人员必须小心使用线程的行动计划。

  5. iOS developer have to manage posiable problems like reuseability of thread, lockings etc. by them self.
  6. iOS开发人员必须自己管理可靠的问题,如线程的可重用性,锁定等。

  7. Thread will consume more memory too.
  8. 线程也会消耗更多内存。

NSOperationQueue:

  1. The NSOperation class is an abstract class which encapsulates the code and data associated with a single task.
  2. NSOperation类是一个抽象类,它封装了与单个任务相关的代码和数据。

  3. Developer needs to use subclass or one of the system-defined subclasses of NSOperation to perform the task.
  4. 开发人员需要使用子类或NSOperation的系统定义子类之一来执行任务。

  5. Add operations into NSOperationQueue to execute them.
  6. 将操作添加到NSOperationQueue中以执行它们。

  7. The NSOperationQueue creates a new thread for each operation and runs them in the order they are added.
  8. NSOperationQueue为每个操作创建一个新线程,并按添加顺序运行它们。

  9. Operation queues handle all of the thread management, ensuring that operations are executed as quickly and efficiently as possible.
  10. 操作队列处理所有线程管理,确保尽可能快速有效地执行操作。

  11. An operation queue executes operations either directly by running them on secondary threads or indirectly using GCD (Grand Central Dispatch).
  12. 操作队列直接通过在辅助线程上运行它们或间接使用GCD(Grand Central Dispatch)来执行操作。

  13. It takes care of all of the memory management and greatly simplifies the process.
  14. 它负责所有内存管理并大大简化了流程。

  15. If you don’t want to use an operation queue, you can also execute an operation by calling its start method. It may make your code too complex.
  16. 如果您不想使用操作队列,还可以通过调用其start方法来执行操作。它可能会使您的代码过于复杂。

How To Use NSThread And NSOperationQueue:

如何使用NSThread和NSOperationQueue:

NSThread:

  1. Though Operation queues is the preferred way to perform tasks concurrently, depending on application there may still be times when you need to create custom threads.
  2. 虽然操作队列是同时执行任务的首选方式,但根据应用程序的不同,您可能仍需要创建自定义线程。

  3. Threads are still a good way to implement code that must run in real time.
  4. 线程仍然是实现必须实时运行的代码的好方法。

  5. Use threads for specific tasks that cannot be implemented in any other way.
  6. 将线程用于无法以任何其他方式实现的特定任务。

  7. If you need more predictable behavior from code running in the background, threads may still offer a better alternative.
  8. 如果您需要从后台运行的代码中获得更多可预测的行为,则线程仍可提供更好的替代方案。

NSOperationQueue:

  1. Use NSOperationQueue when you have more complex operations you want to run concurrently.
  2. 如果要同时运行更复杂的操作,请使用NSOperationQueue。

  3. NSOperation allows for subclassing, dependencies, priorities, cancellation and a supports a number of other higher-level features.
  4. NSOperation允许子类化,依赖性,优先级,取消以及支持许多其他更高级别的功能。

  5. NSOperation actually uses GCD under the hood so it is as multi-core, multi-thread capable as GCD.
  6. NSOperation实际上在引擎盖下使用GCD,因此它像GCD一样具有多核,多线程。

Now you should aware about advantages and disadvantages of NSTread and NSOperation. You can use either of them as per needs of your application.

现在您应该了解NSTread和NSOperation的优缺点。您可以根据应用程序的需要使用其中任何一个。