[IOS Multithreading & Networking-1.0]-Multithreading Overview

Source: Internet
Author: User
Tags gcd

A. ProcessWhat is a process
A process is an application running in the system is independent of each process, each process is running in its dedicated and protected memory space such as Open QQ, Xcode, the system will start 2 processes through "activity monitor" can be viewed in the MAC system open process B. ThreadsThe main thread and the sub-thread occupy memory are 1M and 512K 1 respectively. What is a thread
1 processes to perform a task, you must have a thread (at least 1 threads per 1 processes) threads are the basic unit of execution for a process, and all tasks of a process (program) are performed in thread, such as using a cool dog to play music, downloading a movie with a thunderbolt, and you need to execute 2. Thread serial The execution of a task in 1 threads is serial
If you want to perform more than one task in 1 threads, you can only perform these tasks sequentially, that is, at the same time, 1 threads can perform only one task, such as downloading 3 files in 1 threads (file A, file B, file C) 3. Multithreading What is multithreading
Multiple threads can be opened in 1 processes, each thread can perform different tasks in parallel (at the same time)
Process Workshop, Thread shop worker multithreading technology can improve the execution efficiency of the program, such as opening 3 threads to download 3 files respectively (file A, file B, file C) 4. Multithreading Principle of multithreading principles
At the same time, the CPU can only handle 1 threads, and only 1 threads are working (executing)
Multi-threaded concurrency (simultaneous) execution, in fact, the CPU is quickly dispatched between multiple threads (switching)
If the CPU schedules threads fast enough, it creates the illusion of multi-threaded concurrency execution
Think: What happens if the threads are very, very much?
CPU will be dispatched between N multithreading, the CPU will be exhausted, consumes a lot of CPU resources
The frequency at which each thread is scheduled to execute is reduced (the execution efficiency of the thread is reduced) 5. Multithreading advantages and disadvantages of multithreading
To improve the execution efficiency of the program appropriately
Can appropriately improve resource utilization (CPU, memory utilization)

Disadvantages of multithreading
Open threads need to occupy a certain amount of memory (by default, the main thread takes up 1M, the child threads occupy 512KB), if you open a large number of threads, will consume a lot of memory space, reduce the performance of the program
The more threads, the greater the overhead of the CPU on the dispatch thread
Programming is more complex: such as communication between threads, multi-threaded data sharing 6. Main thread What is the main thread
After an iOS program runs, 1 threads are turned on by default, called the "main thread" or "UI thread"

Main functions of the primary thread
Display \ Refresh UI interface
Handling UI events (such as click events, scrolling events, drag events, etc.)

Note on the use of the main thread
Do not put more time-consuming operations into the main thread time-consuming operation will be stuck in the main thread, seriously affect the smoothness of the UI, give users a "card" bad experience 7.iOS multithreading implementation Technology
    • Pthread
    • Nsthread
    • GCD
    • Nsoperation
C. What needs to be masteredMultithreading
1.NSThread
Several ways to 1> the thread
* First created, then started
Nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Thread start];

* Direct Start
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
[Self Performselectorinbackground: @selector (Run) Withobject:nil];

2> Other uses
Nsthread *current = [Nsthread CurrentThread];
+ (Nsthread *) Mainthread; Get the main thread

3> Inter-thread communication
Performselectoronmainthread .....

2.GCD (Focus)
1> Types of queues
* Concurrent Queue
Get a global concurrent queue: Dispatch_get_global_queue

* Serial Queue
A. Create yourself
Dispatch_queue_create

B. Home row
Dispatch_get_main_queue

2> method types for performing tasks
* Synchronization (Sync) execution
* Asynchronous (Async) execution

3> Understanding how queues and methods work together

4> Inter-thread communication
Dispatch_async (
Dispatch_get_global_queue (Dispatch_queue_priority_default, 0), ^{
Perform time-consuming asynchronous operations ...
Dispatch_async (Dispatch_get_main_queue (), ^{
Go back to the main thread and perform a UI refresh operation
});
});

5> Other uses
Dispatch_once
Dispatch_after
Dispatch_group_async\dispatch_group_notify

3.NSOperation
1> Basic Use
Nsinvocationoperation
Nsblockoperation

2> Nsoperationqueue (Key)
* Maximum concurrent number setting-(void) Setmaxconcurrentoperationcount: (Nsinteger) CNT; Note: This method can only limit the number of simultaneous threads, and cannot limit a specific thread (that is, a different thread group may be executed each time) * Setting Dependencies (interview questions)
[Operationb Adddependency:operationa]; Action B depends on action a

3> Customizing operation (understanding Basic processes)

4> How to resolve a picture (a URL) Repeat download question (interview question)

[IOS Multithreading & Networking-1.0]-Multithreading Overview

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.