MJ-0915-Multithreading 1

Source: Internet
Author: User
Tags gcd

MJ Code level

Demo: Ignore

Learn: Less practice

Mastery: Practice More

Process:

"The system is running an application

"Independent, non-interfering

Thread

"Each process has at least one thread

"Serial, sequential execution

Thread is an execution path for a process

Multithreading

"1 processes can open multiple threads, each thread can perform different tasks in parallel

Improve the efficiency of program execution

"Principle: Multithreading is the illusion that the CPU can only execute 1 threads at the same time, the CPU quickly switches over multiple threads

"iOS Development recommendation 3 about, up to 5

"Problem: too many threads waste resources, but reduce efficiency, impact performance

"Multithreaded programs are complex, communication between threads,

Multi-threaded iOS apps

iOS program starts by default 1 threads, either the main thread or the UI thread, with only 1

Sub-thread: A thread branch created from the main threads

Main thread: Display/Refresh UI interface, handle UI events

The main thread should not perform more time-consuming operations, such as downloading files, loading pictures, storing data

Determine which thread the code is executing on

NSLog (@ "%@", [Nsthread CurrentThread]);

Main thread

NSLog (@ "%@", [Nsthread Mainthread]);

Judging the main thread

if ([Nsthread Ismainthread])

Four ways to implement multithreading in iOS

Pthread C Manual Management No
Nsthread Oc Manual Management Less use
GCD C Automatic Management Common
Nsoperation OC, based on GCD Automatic Management Common

"Thread Status

"1 pthread (C language): No, manual management required

"2 Nsthread (OC language) less use, manual management

Create sub-branch and start automatically

Implicitly starts the thread and executes

Sleep Jam

The thread is destroyed after the interrupt and the start cannot be restarted.

[Nsthread exit]

Multithreading security risks, data confusion

Use a sync lock to solve the problem, the object passed in by the @synchronized (self) can be any unique object, typically using the self

"Pros: Prevent multithreading from robbing the same resource

Cons: Consuming memory and CPU, impacting performance

Thread synchronization: Allow multiple threads to execute on the same line, and use synchronized to resolve them relative to thread concurrency

"Atomic and Nonatomic,atomic Lock the setter method for the property, but it consumes less memory and is not recommended.

"After loading the data, go back to the main thread update Ui,waituntildone to indicate whether to continue executing the following code after executing the current code

"3 GCD (C language) common, automatic management

Grand Central Dispatch, Ox X's Center governor

Multi-core operational optimization Solutions

Canceling the threading concept (automatically managing threads)

C-language function library

Two concepts: Tasks, queues

"Synchronous Dispatch_sync, executed in the current thread, without opening other threads

Asynchronous Dispatch_async, opening new thread execution

"Asynchronous concurrent queue (most commonly used), typically with multiple threads open at the same time

"Asynchronous serial queue (manually created), typically open only one thread

"Asynchronous Home column, no new threads open

Sync Concurrency (logic error, NO)

Synchronization instructions do not open a new thread, so there is no concurrency

"Synchronous serial (NO)

Synchronization is serial execution

"Synchronize the Home row (not available)

Loop wait, unable to perform normally

"4 nsoperation (OC language, based on GCD) commonly used, automatic management

-->>mj-0916-Multithreading 2<<--

"The data types of the foundation and core Foundation frameworks can be converted to each other

Small knowledge points

1. UIButton cannot set image using System mode

2. Attribute names cannot start with new, Init, and are recognized by the compiler as a construction method

3. Only class methods that begin with Initwith can use self

4. Using Xib is a gesture recognizer, the gesture recognizer also exists as an object, so an error occurs when you get the control using the Lastobject method

5. Xib Find Rule

Xib name cannot be the same as controller except controllers

6. Modify the name of the Xib directly in the project and suggest clean the project

MJ-0915-Multithreading 1

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.