What is the difference between 'thread' an

2019-03-13 16:54发布

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!

2条回答
劳资没心,怎么记你
2楼-- · 2019-03-13 17:17

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

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.

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.

查看更多
干净又极端
3楼-- · 2019-03-13 17:18

How NSOperationQueue and NSThread Works:

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 developers have to be careful about a plan of action for using threads.
  3. iOS developer have to manage posiable problems like reuseability of thread, lockings etc. by them self.
  4. Thread will consume more memory too.

NSOperationQueue:

  1. The NSOperation class is an abstract class which encapsulates the code and data associated with a single task.
  2. Developer needs to use subclass or one of the system-defined subclasses of NSOperation to perform the task.
  3. Add operations into NSOperationQueue to execute them.
  4. The NSOperationQueue creates a new thread for each operation and runs them in the order they are added.
  5. Operation queues handle all of the thread management, ensuring that operations are executed as quickly and efficiently as possible.
  6. An operation queue executes operations either directly by running them on secondary threads or indirectly using GCD (Grand Central Dispatch).
  7. It takes care of all of the memory management and greatly simplifies the process.
  8. 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.

How To Use NSThread And 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. Threads are still a good way to implement code that must run in real time.
  3. Use threads for specific tasks that cannot be implemented in any other way.
  4. If you need more predictable behavior from code running in the background, threads may still offer a better alternative.

NSOperationQueue:

  1. Use NSOperationQueue when you have more complex operations you want to run concurrently.
  2. NSOperation allows for subclassing, dependencies, priorities, cancellation and a supports a number of other higher-level features.
  3. NSOperation actually uses GCD under the hood so it is as multi-core, multi-thread capable as 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.

查看更多
登录 后发表回答