Summary of Multithreading Knowledge in iOS (I.)

Source: Internet
Author: User

This section of the development has been dealing with iOS multi-threading problem, but the sense of knowledge is too loose, so the iOS in the multi-threading knowledge points.

1. Basic Concepts

1) What is a process? What is the nature of the process ?

A process is an application that is running in the system.

Features: Each process is independent, and each process runs within its dedicated and protected memory space.

2) What is a thread? What is the relationship between threads and processes?

A process that wants to perform a task must have a thread (at least one thread per process), which is the basic unit of the process, and all tasks in a process are executed in the thread.

Relationship: Process contains thread

3) What is multithreading?

Multiple threads are opened in one process, and each thread can perform different tasks in parallel. For example: When performing multiple download tasks at the same time

4) What is thread parallelism? What is thread serial? What is the principle of thread parallelism?

Thread parallelism: Executing multiple threads at the same time

Thread Serial: The execution of a task in a thread is serial, that is, a thread can only perform one task at a time

Thread parallelism principle: At the same time, the CPU can only handle one thread, and only one thread is working. The principle is that the CPU is dispatched between multiple threads, and if the CPU schedules the thread fast enough, it creates the illusion of multi-threaded concurrency execution.

5) Advantages and disadvantages of using multithreading

Advantages: 1) Can improve the execution efficiency of the program properly

2) to improve resource utilization appropriately

Cons: 1) The open thread needs to occupy a certain amount of memory space, if you open a large number of threads, it will occupy a lot of memory space. Reduce program execution efficiency

2) The more threads, the greater the overhead of the CPU on the dispatch thread

3) programming more complex, need to deal with the communication between threads, multi-threaded data sharing

6) What is the main thread of the main thread?

Main thread: Once an iOS program is running, the default is to turn on one of the threads, called the "main thread" or "UI thread"

Function: Refreshes the display UI, handles UI events

Implementation of multi-threading in 2.iOS

    There are four implementations of multi-threading in iOS: (1) pthread (in C, almost unused) (2) Nsthread (occasionally used) (3) GCD (often used) (4) Nsoperation (lower level is GCD, but more object-oriented)

1) Pthread

  This thread-on-the-go approach is relatively low-level and rarely used in iOS development, and does not introduce

2) Nsthread

(1) Features:

Use more object-oriented

Easy to use, direct manipulation of thread objects

(2) Declaration period:

Managed by a programmer

(3) Nsthread Four ways to create

01. Create a Thread object

// 01 Creating a Thread object    /*      First parameter: target object     second parameter: Method selector     The third parameter: The parameter     passed to the method selector in the method picker *    / // Advantage: The disadvantage of being able to get the thread object: the need to start the thread manually    object:@ "iOS"];     // 02 Start Thread    [Thread start];

02. Create a thread

// Method Two     // Pros: Auto-start thread disadvantage: Cannot get thread object    [Nsthread detachnewthreadselector: @selector (run:) totarget:self Withobject:nil];

03. Open a Background thread

// method Three opens a background thread     // Pros: Auto-start thread disadvantage: Cannot get thread object    [Self Performselectorinbackground: @selector (run:) Withobject:@ " background thread "];

04. Custom Create Thread

// method Four Customization    Newthread *threadb = [[Newthread alloc]init];    [threadb start];

(4) Setting the properties of the Nsthread

Setting a property comparison is often a name and priority. The setting name can be set by its Name property, and the priority of the thread is set by the ThreadPriority property. The code is as follows:

// Set name     @" Thread A " ; // Set Priority 0~1.0 default is 0.5 max is 1.0     1.0 ;      0.01;

By setting the priority property of a thread, you can implement a thread to take precedence.

(5) Controlling the state of the thread

// Common ways        to control the state of a thread [Nsthread exit]; // exit Current thread        [Nsthread sleepfortimeinterval:2.0]; // Blocking Threads        [Nsthread sleepuntildate:[nsdate Datewithtimeintervalsincenow:2.0]]; // Blocking Threads

GCD and Nsoperation put it in the next chapter and write again.

Summary of Multithreading Knowledge in iOS (I.)

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.