iOS Multithreading Introduction _java

Source: Internet
Author: User

I. Foreword part

Recently in the interview, to review more than one thread, hope to deepen the understanding of multithreading.

1, what is the process?

1. To understand threads we must first understand the process, in layman's terms, the process is an application running in the system.

2. Each thread is independent and runs in its dedicated and protected memory space.

3. For example, open QQ or Xcode system will open two separate processes as shown:

4, we can view the process that is open in Mac system through "Activity monitor".

2, what is a thread?

1. A process must have a thread in order to perform a task, that is, a process should have at least one.

2. A thread is the basic unit of execution of a process, and all the tasks of a process (program) are performed in a thread.

3. For example, the use of cool dogs to play music, the use of thunder to download movies are required to run in the thread as shown:

3, what is the serial of the thread?

1. The tasks in a thread are executed serially (sequentially), that is, a thread can perform only one task at a time.

2. Serial execution diagram, such as one thread downloading 3 files (files A, B, C)

4, what is multithreading?

1. Multiple threads can be opened in a process, and each thread can perform different tasks concurrently (simultaneously).

2. Similar relationship enumeration: process----> Workshop; thread----> Workshop workers

3. Multithreading diagrams, such as opening 3 threads at the same time download 3 files separately (files A, B, C)

5. Multithreading principle

1. The CPU can only execute one thread at a time, and only one thread is working (executing).

2. Multithreading concurrency (simultaneous) execution, in fact, is the CPU quickly between multiple threads scheduling (switching).

3. If the CPU dispatches a thread fast enough, it can create the illusion of concurrent execution of multithreading.

4). The disadvantages of Multithreading:

1, each thread will occupy a certain amount of memory space (by default: The main thread occupies 1MB, child threads occupy 512KB),

If the opening of the thread too much will occupy a lot of memory space, resulting in lower program performance.

2, the more the thread the more CPU scheduling thread on the more expensive (similar factory workers, the more the factory overhead).

3, make the program design more complex: For example, multithreading data communication, multithreading between the sharing of information.

5). The advantages of Multithreading:

1, can appropriately improve the implementation of the program efficiency.

2, can appropriately improve the utilization of resources (CPU, memory utilization)

6, what is the main thread?

1. When an iOS program is opened, a thread is opened by default, which is called the "main thread" or "UI thread".

2). Main function of Master thread:

1. Display/Refresh UI interface

2, handling UI events (such as click events, scrolling events, drag events, etc.)

3). Main thread attention point:

1, do not put the time-consuming operation in the main thread, time-consuming operation in the main thread will cause the problem of the program cotton.

7, time-consuming Operation demo Demo

1), directly in the main thread running the demo

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{
//Get current execution method and current thread
//number = = Main thread
//number!= other threads, child threads, secondary threads
NSLog (@ "%s----%@", __func__,[nsthread CurrentThread]);
Run directly in the main thread causing UI operations cotton
[self longtimeoperation];
}
#pragma mark-time-consuming operation
-(void) longtimeoperation{
for (int i=; i<; i++) {
NSLog (@ "%d", I);
}

2), running in the child thread demo

-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (uievent *) event{
//Get current execution method and current thread
// number== main thread
//number!= other threads, child threads, secondary thread
NSLog (@ "%s----%@", __func__,[nsthread CurrentThread]);
Putting time-consuming operations on child threads does not affect the operation of the UI
[self Performselectorinbackground: @selector (longtimeoperation) withobject:nil];
}
#pragma mark-time-consuming operation
-(void) longtimeoperation{
for (int i=; i<; i++) {
NSLog (@ "%d", I);
}

The above content is small to introduce the iOS multithreading knowledge, I hope to help!

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.