Turn multithreading Supplement

Source: Internet
Author: User

Processes and Threads

  To use multi-threading, you first need to understand the 2 concepts of processes and threads. Here I simply say.   The so-called process corresponds to an application that is responsible for opening up memory space for application use, but the process cannot perform tasks (directives). A process contains at least one thread, and the thread is the execution flow of the program. When the   iOS program starts, it starts running a thread while creating a process, which is called the main thread • Each process in the system has its own independent virtual memory space, while multiple threads in the same process share the memory space of the process • Every time a new thread is created, Consumes a certain amount of memory and CPU time • When multiple threads compete for the same resource, you need to be aware of thread safety issues   multi-threaded applications and usage considerations to assign time-consuming, polling, or high-concurrency requirements to other threads to execute, And the unified update interface by the main thread will make the application smoother and the user experience better, such as network requests, playing game background music and so on. • Thread usage is not uncontrolled the stack size of the main thread in –ios is 1m– starting from the second thread is 512kb– these values cannot be changed by the compiler switch or the thread API function Note: Update UI interface, interaction between processing interface and user events must be handled in the main thread. Multithreading techniques in  ios  1.nsobject multithreading-nsthread encapsulation (one thread for a Nsthread object)   How to open a background thread to perform a task-(void) Performselectorinbackground: (SEL) Aselector withobject: (ID) arg notifies the main thread of the method of performing a task in the background threads – (void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait; get thread information [nsthread currentthread]; thread hibernation      [nsthread sleepfortimeinterval:1.0f]; Tips: – The Performselectorinbackground method itself is executed in the main thread, and the selector specifies that the method is done in a background thread – although a task invoked with the Performselectorinbackground method can update the UI interface, However, in actual development, it involves updating the UI interface or using the Performselectoronmainthread method to avoid having toFor the trouble. Note: • Memory management is important for multithreading Objective-c can use memory resources with @autoreleasepool and reclaim resources when needed • A memory leak may occur if each thread requires @autoreleasepool 2.nsthread  Create Thread method: 1.+ (void) Detachnewthreadselector: (SEL) selector totarget: (ID) target withobject: (ID) argument;2.-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument; parameter Description: –selector: Method of thread execution, There can be only one parameter, no return value –target:selector the object sent by the message –argument: the only parameter transmitted to target, or it can be nil 3.nsoperation/nsoperationqueue Two subcategories of   nsoperation 1.nsinvocationoperation2.nsblockoperation  How it works: 1. Encapsulate the action to be performed with the Nsoperation 2. Place the created Nsoperation object in Nsoperationqueue 3. Start Operationqueue start a new thread execution queue operation · Note: 1. When using multi-threading, it is often necessary to control the number of concurrent threads, because threads consume system resources, run too many threads, and the system slows down by 2. Use the following method to control the number of concurrent threads:-(void) Setmaxconcurrentoperationcount :(Nsinteger) cnt;3. Use adddependency to establish a dependency between operations, set the order in which the operations are executed    the above program set dependencies, and only after the operations A and B are executed, C. 4 will be executed. gcd  gcd Queue: 1. Global queue: All tasks added to the primary queue are executed concurrently (multiple threads may be turned on) dispatch_queue_t queue = Dispatch_get_global_queue (Dispatch_ Queue_priority_default, 0); 2. Serial Queue: All Tasks added to the serial queue are executed sequentially (only one thread may be turned on) dispatch_queue_t queue = dispatch_queue_create ("Myqueue", dispatch_queue_serial); 3. Primary queue: All tasks added to the primary queue are dispatch_get_main_queue () executed in the main thread,   asynchronous operations –dispatch_async perform tasks on other threads, and new threads are opened- Async methods cannot determine the order in which tasks are executed • Synchronous Operation –dispatch_sync Performing tasks on the current thread without opening new threads – synchronous operations are not queue-independent – the synchronization method executes sequentially and determines the order in which tasks are executed – it is best to use synchronous methods when updating the interface UI Note: synchronous or asynchronous , depending on the method name (without affecting the home column, affecting the global queue, serial queue).

To multithreading Supplement

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.