Introduction to iOS-multithreading and ios-Multithreading

Source: Internet
Author: User

Introduction to iOS-multithreading and ios-Multithreading
I. Preface

I recently reviewed multiple threads in an interview, hoping to deepen my understanding of multiple threads.

1. What is a process?

1) to understand the thread, we must first understand the process. Generally speaking, the process is an application running in the system.

2) Each thread exists independently and runs in its dedicated and protected memory space.

3). For example, the QQ or Xcode system will start two processes respectively.

4) we can view the processes enabled in the Mac system through "activity monitor.

2. What is a thread?

1) A process must have a thread to execute a task, that is, a process must have at least one thread.

2). A thread is the basic execution unit of a process. All tasks of a process (Program) are executed in a thread.

3). For example, playing music with codoy and downloading movies with thunder all need to run in the thread.

3. What is thread serial?

1). tasks in a thread are executed serially (sequentially). That is to say, a thread can only execute one task at a time.

2) serial execution diagrams, such as downloading three files (Files A, B, and C) in one thread)

4. What is multithreading?

1). Multiple Threads can be enabled in a process, and each thread can execute different tasks concurrently (at the same time.

2) List similar links: Process ----> workshop; thread ----> Workshop worker

3) multi-thread illustration. For example, you can enable three threads to download three files (File A, file B, and file C) at the same time)

 

5. multithreading Principle

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

2). multi-threaded concurrent (simultaneous) execution is actually a fast CPU scheduling (switching) between multiple threads ).

3) if the speed of the CPU scheduling thread is fast enough, it will create the illusion of multi-thread concurrent execution.

4). disadvantages of multithreading:

1. Each thread occupies a certain amount of memory space (by default, the main thread occupies 1 MB, and the sub-thread occupies KB ),

If too many threads are enabled, a large amount of memory space will be occupied, thus reducing program performance.

2. The more threads there are, the larger the overhead of the CPU scheduling thread (similar to the more factory workers, the larger the overhead of the factory ).

3. Make the program design more complex: for example, multi-threaded data communication and data sharing among multiple threads.

5) Advantages of multithreading:

1. The program execution efficiency can be appropriately improved.

2. Resource utilization can be appropriately improved (CPU and memory usage)

6. What is a main thread?

1). A thread is enabled by default after an iOS program is enabled. This thread is called "main thread" or "UI thread ".

2). Main functions of the main thread:

1. Display/refresh the UI

2. Process UI events (such as click events, rolling events, and drag and drop events)

3) main thread notes:

1. Do not place time-consuming operations in the main thread. Time-consuming operations in the main thread may cause program freezing.

7. Demo of time-consuming operations

1) Demo run directly in the main thread

1-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {2 // obtain the current execution method and current thread 3 // number = 1 main thread 4 // number! = 1 other threads, subthreads, and secondary threads 5 NSLog (@ "% s ---- % @" ,__ func __, [NSThread currentThread]); 6 7 // UI operation lagging due to direct running in the main thread 8 [self longTimeOperation]; 9} 10 11 # pragma mark-time-consuming operation 12-(void) longTimeOperation {13 for (int I = 0; I <20000; I ++) {14 NSLog (@ "% d", I); 15} 16}View Code

2) Demo running in sub-Thread

1-(void) touchesBegan :( NSSet <UITouch *> *) touches withEvent :( UIEvent *) event {2 // obtain the current execution method and current thread 3 // number = 1 main thread 4 // number! = 1 other threads, subthreads, and secondary threads 5 NSLog (@ "% s ---- % @" ,__ func __, [NSThread currentThread]); 6 7 // place the time-consuming operation in the Child thread for execution without affecting the UI operation 8 [self defined mselectorinbackground: @ selector (longTimeOperation) withObject: nil]; 9} 10 11 # pragma mark-time-consuming operation 12-(void) longTimeOperation {13 for (int I = 0; I <20000; I ++) {14 NSLog (@ "% d", I); 15} 16}View Code

 

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.