[IOS multithreading & amp; network, ios Multithreading

Source: Internet
Author: User

[IOS multithreading & Network, ios Multithreading
A. ProcessWhat is a process?
A process is an application running in the system. Each process is independent of each other. Each process runs in its dedicated and protected memory, such as opening QQ and Xcode at the same time, the system starts two processes separately. The "activity monitor" allows you to view the processes enabled in the Mac system.B. threadsThe memory occupied by the main thread and sub-thread is 1 MB and 512 KB respectively. What is the thread?
To execute a task, a process must have a thread (each process must have at least one thread). The thread is the basic execution unit of the process, and a process (Program) all tasks are executed in the thread. For example, playing music with a cool dog and downloading movies with thunder require 2. thread serial: the execution of tasks in one thread is serial.
If you want to execute multiple tasks in one thread, you can only execute these tasks one by one in order, that is, within the same time, one thread can only execute one task. For example, three files (File A, file B, and file C) can be downloaded in one thread. multithreading what is Multithreading
Multiple Threads can be enabled in one process, and each thread can execute different tasks in parallel (at the same time ).
In the Process Workshop, the thread workshop's multi-thread technology can improve the program execution efficiency, for example, enabling three threads to download three files (File A, file B, and file C) at the same time. multithreading principles multithreading principles
At the same time, the CPU can only process one thread, and only one thread is working (executed)
Multi-thread concurrent (concurrent) execution is actually a fast CPU scheduling (switching) between multiple threads)
If the CPU scheduling thread time is fast enough, it creates the illusion of multi-thread concurrent execution.
Think: what happens if there are many threads?
The CPU will be scheduled between N threads, and the CPU will be exhausted, consuming a lot of CPU resources
The frequency of scheduled execution of each thread is reduced (the thread execution efficiency is reduced) 5. Advantages and Disadvantages of Multithreading
Appropriately improves program execution efficiency
Resource utilization can be appropriately improved (CPU and memory usage)

Disadvantages of Multithreading
To enable a thread, a certain amount of memory space is required (by default, the main thread occupies 1 MB, and the sub-thread occupies KB). If a large number of threads are enabled, a large amount of memory space is occupied, program Performance reduction
The more threads, the higher the CPU overhead on the scheduling thread.
Program Design is more complex: for example, communication between threads and data sharing among multiple threads 6. What is the main thread?
After an iOS program runs, one thread is enabled by default, which is called "main thread" or "UI thread"

Main functions of the main thread
Display \ refresh UI
Process UI events (such as click events, rolling events, and drag-and-drop events)

Usage of the main thread
Do not place time-consuming operations in the main thread. Time-consuming operations will get stuck in the main thread, seriously affecting the smoothness of the UI, giving users a "card" bad experience 7. iOS multithreading Implementation Technology

  • Pthread
  • NSThread
  • GCD
  • NSOperation
C. What you need to knowMultithreading
1. NSThread
1> several methods of thread opening
* First create and then start
NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
[Thread start];

* Start directly
[NSThread detachNewThreadSelector: @ selector (run) toTarget: self withObject: nil];
[Self defined mselectorinbackground: @ selector (run) withObject: nil];

2> other usage
NSThread * current = [NSThread currentThread];
+ (NSThread *) mainThread; // obtain the main thread

3> inter-thread Communication
Performselecdomainmainthread .....

2. GCD (important)
1> queue type
* Concurrent queue
Obtain the global concurrency queue: dispatch_get_global_queue

* Serial queue
A. Create your own
Dispatch_queue_create

B. Main Columns
Dispatch_get_main_queue

2> method type of task execution
* Sync execution
* Asynchronous (async) Execution

3> understand how queues and methods work together

4> inter-thread Communication
Dispatch_async (
Dispatch_get_global_queue (DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^ {
// Execute time-consuming asynchronous operations...
Dispatch_async (dispatch_get_main_queue (), ^ {
// Return to the main thread and perform the UI refresh operation
});
});

5> other usage
Dispatch_once
Dispatch_after
Dispatch_group_async \ dispatch_group_policy

3. NSOperation
1> basic usage
NSInvocationOperation
NSBlockOperation

2> NSOperationQueue (important)
* Set the maximum number of concurrent threads-(void) setMaxConcurrentOperationCount :( NSInteger) cnt; Note: This method can only limit the number of concurrent threads, specific threads cannot be restricted (the thread groups may be different each time) * Set dependencies (interview questions)
[OperationB addDependency: operationA]; // operation B depends on operation

3> Custom Operation (basic procedures)

4> how to solve the problem of repeated download of an image (a url) (interview question)

Related Article

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.