iOS Development Note 5: Multi-thread Nsthread, nsoperation, and GCD

Source: Internet
Author: User
Tags gcd

This article mainly summarizes the use of multi-threading in iOS development, multithreading development generally using Nsthread, nsoperation and gcd Three ways, commonly used GCD and nsoperation.

1.NSThread

There are three main ways of creating a thread:

It is important to note that when multiple threads may access an object at the same time, use "@synchronized" to resolve such thread synchronization issues.

Notifying the main thread in a child thread typically uses the following two ways:

2.NSOperation

There are three ways to use Nsoperation, are nsinvocationoperation, Nsblockoperation, as well as custom nsoperation operations, and the ability to set dependencies on Nsoperation, to determine the order in which tasks are executed, to monitor and even control the state of the task, and to set the maximum number of concurrent threads and so on.

(1) nsinvocationoperation

The first is the nsinvocationoperation way to create the thread, it is important to note that this method is created only to join the queue to perform the asynchronous operation, or in the main thread synchronous execution

when joined to the queue for automatic execution, a new thread is executed asynchronously

(2) nsblockoperation

The second is to use nsblockoperation, and it is also important to note that when the number of tasks in the operation is only one, the task is executed synchronously in the current thread

When the number of tasks is greater than 1, a new thread is opened and executed asynchronously

When a nsblockoperation defined operation is added to the queue, these tasks are performed asynchronously and concurrently

(3) inherit nsoperation, custom operation

This approach is more flexible, by inheriting nsoperation and overriding related methods, such as Sdwebimage.

(4) Setting dependencies

If there are sequential dependencies between tasks, you can set dependencies on them to solve the problem, Operation1 relies on operation2, and Operation2 relies on operation3, so the order of execution is Operation3, Operation2 , Operation1

(5) Set the maximum number of concurrent tasks

You can limit the number of concurrent tasks by setting maxconcurrentoperationcount and avoid consuming too many resources for concurrent operations

(6) Monitoring and setting of task status

You can get the execution status of the task, whether it is canceled or completed, and so on.

It is important to note that the task does not necessarily cancel the message of canceling the task, because after joining the queue, the execution of the task is managed by the system, and the cancellation succeeds if the task has not been executed yet, and it is important to note that even if the task is canceled successfully, Also will be identified as the task has been completed finished (specific), so if there are other tasks dependent on the task has been canceled, need to use "iscancelled" to judge, especially the task completion callback event to do some operations, may say a bit around, see in detail:

If you do not judge, even if Operation3 has been canceled, because the system thinks the canceled task is also finished, so Operation3 's task completion callback event is still triggered

Refer to this section of concurrency Programming Guide in more detail

Of course, you can also manage the entire column directly, including suspending or resuming all tasks in the queue, canceling all tasks in the queue, and so on.

3.GCD

The use of GCD is mainly in the following ways:

(1) in the main thread to add tasks,Dispatch_async Asynchronous execution, the common usage scenario is to enter after the asynchronous loading of related picture information

(2) Troubleshooting thread synchronization in GCD the serial queue is used, and the tasks in the serial queue are executed sequentially in order of addition, and it is important to note that the tasks are performed in a distpatch_sync synchronous manner, and all tasks are performed on the main thread

(3) in the serial queue to Dispatch_async Execute a task asynchronously, the system opens a new thread to perform the task

(4) In a concurrent queue, the task is executed asynchronously in Dispatch_async, and the system creates a certain number of threads to perform the task based on the number of tasks, noting that the global concurrency queue is used here.

(5) GCD way to perform deferred tasks

Perform a deferred task for returning to the main thread, performing a deferred task for opening a new threads

(6) Queue Group

When you need to perform multiple tasks at the same time, such as downloading multiple pictures at the same time and processing the pictures to show similar scenarios, you can perform tasks concurrently through queue groups to improve efficiency and use

(7) Solve the problem of thread synchronization in addition to using the serial queue, you can also use the semaphore to solve, detailed reference concurrency Programming Guide, in addition to note that the following two ways of use will create a problem

One is to perform a task in the main thread with Dispatch_sync synchronization deadlock

The other is to perform tasks in parallel queues in Dispatch_sync synchronization, losing the meaning of concurrency, and ultimately all tasks are executed one at a time in the main thread

4. Summary

The general situation with nsoperation and GCD can meet the application needs, GCD way to use is concise enough, and nsoperation use is more flexible and convenient, can monitor the task execution status, cancel the task, set the maximum number of concurrent tasks and so on.

iOS Development Note 5: Multi-thread Nsthread, nsoperation, and GCD

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.