----multithreading of the questions in iOS

Source: Internet
Author: User

1. The bottom-up implementation of multithreading

(1) First answer what is thread

1 processes to perform a task, you must have a thread. Threads are the basic execution unit of a process, and all tasks of a process (program) are executed in the thread

(2) What is multithreading

Multiple threads can be opened in 1 processes, and each thread can perform different tasks in parallel (at the same time). The role of Multithreading: Updating the Display UI interface, handling user touch events. (3) Mach is the first system to handle tasks in a multi-threaded manner, so the underlying implementation mechanism for multithreading is Mach-based threading. (4) Implementation of multithreading in the development of scenario 1 "C language POSIX interface: #include <pthread.h> 2" OC Nsthread 3 "C Language GCD interface (performance best, code more streamlined) 4" OC NSO Peration and Nsoperationqueue (based on GCD) 2. Thread Communication (1) GCD:

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

Download image

UIImage *image = nil;

Dispatch_async (Dispatch_get_main_queue (), ^{

Back to main thread

});

(2) Nsthread thread communication

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

Download image

UIImage *image = nil;

[Self performselector: @selector (settingimage:) onthread:[nsthread Mainthread] Withobject:image waituntildone:yes Modes:nil];

}

This situation also applies to communication between child threads.

(3) Nsthread of thread Communication

Dispatch_async (Dispatch_get_global_queue (dispatch_queue_priority_default, 0), ^{

Download image

UIImage *image = nil;

[Self Performselectoronmainthread: @selector (settingimage:) withobject:image Waituntildone:yes];

}

3. How to solve the problem of a duplicate request of the same network address in the Network picture processing problem

Using the dictionary image address as key, the download operation is value

4. Enumerate the implementation of several multithreading in cocoa, and discuss several methods of multithreading security and how to control multithreading security?

1 "Multi-threaded solution in development

1 "C language POSIX interface: #include <pthread.h> 2" OC Nsthread 3 "C Language GCD interface (best performance, code more streamlined) 4" OC Nsoperation and Nsoperatio     Nqueue (GCD) 2 "Multithreaded security solution 1" only in the main thread refresh Access UI 2 "If you want to prevent resource looting, you have to use synchronize for lock protection. 3 "If the asynchronous operation to ensure the line Cheng and other issues, try to use GCD.  (GCD some functions by default is safe) 5.GCD internal how to achieve 1 "iOS and OSX core is the XNU kernel (Apple Computer development operating system kernel), GCD is based on the XNU core implementation. 2 "GCD API all in Libdispatch Library 3" GCD the underlying implementation of the main is: Dispatch queue and Dispatch source Dispatch queue: Management block operation Dispatch Source: processing Events (such as communication between threads) 6. GCD and Nsoperationqueue 1 "GCD is a pure C language Api,nsoperationqueue is based on the GCD of the OC version of the package 2" GCD only support FIFO queue, Nsoperationqueue can easily adjust the order of execution,   You can add dependent settings for maximum concurrent quantities. 3 "GCD faster than Nsoperationqueue 4" nsoperationqueue support KVO, you can detect whether operation is executing, whether the end, whether canceled. How do I make a selection? Tasks are not dependent on each other, choose GCD, have dependencies between tasks, or listen to the execution of tasks: Nsoperationqueue

----multithreading of the questions in iOS

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.