iOS Development--Multithreading OC & (i) Multithreading introduction

Source: Internet
Author: User

Multithreading Simple Introduction

I. Processes and Threads

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

You can view the processes that are open in your Mac system through Activity Monitor

2. What is a thread

1 processes to perform a task, a thread must be wired (at least 1 threads per 1 processes)

Threads are the basic unit of execution of a process, and all tasks of a process (program) are executed in the thread

For example, using a cool dog to play music, using Thunder to download movies, all need to execute 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)

Second, multi-threading

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-shop 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. The principle of multithreading

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 (thread execution efficiency is reduced)

3. Advantages and disadvantages of multithreading

Advantages 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

4. Multithreading in the development of iOS application

Main thread: After an iOS program runs, the default is to turn on 1 threads, 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.)

Use of the main thread note: Do not put more time-consuming operations into the main thread.

Time-consuming operations can get stuck in the main thread, seriously affecting the smoothness of the UI, giving users a "card" bad experience

5. Code examples

1 //2 //YYVIEWCONTROLLER.M3 //01-Block main thread4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten  One @interfaceYyviewcontroller () A-(ibaction) Btnclick; - @end -  the  - @implementationYyviewcontroller -  -  +- (void) Viewdidload - { + [Super Viewdidload]; A } at  -  - //button's Click event --(ibaction) Btnclick { -     //1. Get the current thread -Nsthread *current=[Nsthread CurrentThread]; in     //2. Use a For loop to perform some time-consuming operations -      for(intI=0; i<10000; i++) { to         //3. Output Thread +NSLog (@"Btnclick---%d---%@", i,current); -     } the } *  $ @end

Note: When clicking Execute, textview click No response.

Perform analysis: Wait for the main thread to execute serially.

Turn on the child thread.

iOS Development--Multithreading OC & (i) Multithreading introduction

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.