Learn iOS Multithreaded development demo

Source: Internet
Author: User

Concurrency describes the concept of running multiple tasks at the same time. These tasks may be run concurrently in the form of ticks (time sharing) on a single core CPU, or in a true parallel way on multicore CPUs. This article is the author of the development of iOS technology, when encountering multi-threading problems when smoothing the idea of some ideas.

GCD is a technology that solves multi-core development in iOS development.

There are three main types of queues: one, main queque running on the main thread to get through Dispatch_get_main_queque

Second, parallel queue Global dispatch Queque, obtained through Dispatch_get_global_queque. Parallel queues have high priority, default, and low.

Serial queue serial queues is typically used to synchronize access sequentially, creating any number of serial queues that are concurrent between each serial queue. serial queues are useful when you want tasks to execute in a particular order. A serial queue performs only one task at a time. We can use serial queues instead of locks to protect shared data. Unlike locks, a serial queue guarantees that the task executes in a predictable order.

Serial queues is created through dispatch_queue_create and can be used to increase or decrease the reference count using function Dispatch_retain and dispatch_release.

          

  

One: The problem description: Take a string, convert it all to uppercase, and then calculate the length of the string and replace some characters. The purpose of the demo is to find out how multi-Threading technology can reduce the specific running time, and to demonstrate the correct use of multithreading technology can greatly improve the efficiency of code execution.

Two: Problem analysis: First to get a string, this task should be the first to complete. Convert the string all to uppercase, calculate the length, replace the relevant position three actions are unrelated. Therefore, the sequential relationship between the two should be made into a serial queue, three actions are divided into parallel queues.

Three: Comparison of test results:

Figure 1 (using GCD technology)

Figure 2 (gcd not used)

First, compare the time spent on two different methods (multi-threaded versus non-multithreaded): Figure 1 uses GCD technology to achieve demo spending time: Figure 2 Using non-GCD technology to achieve demo time = 1:10.

This small piece of code before and after the running time of the terrorist differences, multithreading to improve the efficiency of code execution is visible.

Figure 3 (GCD)

Note: The scope range of the 1,block block variable

2, the group of three action processing objects are fetchdata, this must grasp clearly. Parallel queues are executed randomly, so three actions (convert uppercase, calculate length, substitution character) are placed in the same group, not necessarily first,

When testing the demo, because it was a non-GCD case, when refactoring the code, I did not change the two object (length substitution character) method from Handledata (convert uppercase) to Fetchdata (data), and I did not fully understand the order of execution of the parallel queue Thought it would also be sequential execution three the same level there are two that are dependent on the first processing result and a logical error has occurred.

3, the entire page of the refresh display, should be placed in the main thread.

Figure 4 (non-GCD)

Note: The basic order is executed.

The above content has the reference http://objccn.io/issue-2-1/

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.