Cat learning iOS (48) Multithreaded network multi-Threading Simple understanding

Source: Internet
Author: User

Cat Share, must boutique

Original articles, welcome reprint. Reprint Please specify: Sanayu's Blog
Address: http://blog.csdn.net/u013357243?viewmode=contents

One: Process and thread 1: what is a process

A process is an application that is running in the system
Each process is independent, and each process runs within its dedicated and protected memory space.


For example, open QQ, Xcode, the system will start 2 processes respectively.

The Activity Monitor lets you view the processes that are turned on in your Mac system.

2: What is a thread

1 processes to perform a task, you have to have a thread (at least 1 threads per 1 processes-the primary thread).
A thread is the basic unit of execution for a process, and all the tasks of a process (program) are executed in threads.

For example, using a cool dog to play music, using Thunder to download movies, all need to be executed in the thread.

3: Thread's serial

The execution of a task in 1 threads is serial
If you want to perform multiple tasks in 1 threads, you can perform these tasks sequentially, one at a-
That is, at the same time, 1 threads can only perform one task

For example, download 3 files in 1 threads (file A, file B, file C, respectively)

Two: Multithreading 1: 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? Workshop workers
Multithreading technology can improve the efficiency of program execution

For example, the simultaneous opening of 3 threads to download 3 files (file A, file B, file C, respectively)

2: Advantages and disadvantages of multithreading

(1) Advantages of Multithreading:

A: Can properly improve the execution efficiency of the program.
B: can appropriately improve resource utilization (CPU, memory utilization).

(2) Disadvantages of multithreading
A: The open thread needs to occupy a certain amount of memory space (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.
B: The more threads, the more overhead the CPU will have on the dispatch thread.
C: Programming is more complex: such as communication between threads, multi-threaded data sharing.

Three: The application of Multithreading in iOS Development 1: What is the main thread

After an iOS program runs, the default is to turn on 1 threads, called the "main thread" or "UI thread."

(1) Main function of primary thread
A: Displays \ Refreshes the UI interface.
B: Handle UI events (such as click events, scrolling events, drag events, and so on).

(2) Attention to the use of the main thread
A: Do not put more time-consuming operations into the main thread.
B: The time-consuming operation will get stuck in the main thread, seriously affecting the smoothness of the UI, giving the user a "card" bad experience.

Give me a chestnut.

If you put the time-consuming operation on the main thread, assuming that some time-consuming operation takes a long way, then we operate, and in the main thread she's stuck for 10 seconds, this user experience .... A weak explosion.

Code:

#import "NYViewController.h"  @interface nyviewcontroller ()- (ibaction) Btnclick;@end @implementation nyviewcontroller - (void) viewdidload{[SuperViewdidload];additional setup after loading the view, typically from a nib.}- (void) didreceivememorywarning{[SuperDidreceivememorywarning];//Dispose of any resources, can be recreated.}- (ibaction) Btnclick {//1. Get the current thread    Nsthread*current = [NsthreadCurrentThread];//2. Take some time-consuming actions     for(inti =0; i<10000; i++) {//3. Output Thread        NSLog(@"%@", current); }}@end

Effect:

The problem is, we ordered the button, but I want to operate all the time I got a daisy ...

Current execution analysis: Waits for the main thread to execute serially.

Solution:

Turn on the child thread.

Multi-threading implementations in iOS

Cat learning iOS (48) Multithreaded network multi-Threading Simple understanding

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.