Thread Learning Notes

Source: Internet
Author: User
Tags gcd

Multi-threaded Network learning

What is a process?

An application that is running in the system is a process.

For example, the simultaneous opening of the QQ Xcode system will start two processes respectively. And it is a relatively independent process that does not affect each other.

1 processes want to perform a task, they must have a thread, and each process has at least 1 threads.

A thread is the basic execution unit of a process, and all tasks of a process are executed in a thread.

Serial:

The tasks in the 1 threads are serial (sequential execution).

If you perform multiple tasks in 1 threads, you can perform these tasks sequentially, one by one.

In other words, 1 threads can only perform one task at a time.

such as downloading tasks.

Multithreading

In 1 processes, multiple threads can be opened, and each thread can perform different tasks concurrently (concurrently).

For example: The computer can open the button chat, can listen to music at the same time, browse the Web and so on.

Multithreading principle:

At the same time, the CPU can only handle 1 threads, and only one thread is working (executing).

Threads execute concurrently, in fact, the CPU quickly switches between multiple threads. --that is, each thread executes a bit at a time until all processes are executed.

If the CPU schedules the thread to be fast enough, it creates the illusion of multi-threaded concurrency execution.

Think: What happens if there are a lot of threads?

CPU will switch directly on n threads, CPU will be exhausted and consume a lot of CPU resources

Advantages of multithreading

1. Improve program execution efficiency

2. Can properly improve resource utilization

Multithreading Disadvantages

1. Open threads need to occupy a certain amount of memory space (by default the main thread takes up 1m child threads to occupy 512MB) if you open a large number of threads, it consumes a lot of memory space and reduces the performance of the program.

2. The more threads, the higher the CPU overhead on the scheduling thread.

3. The programming is more complex: such as the communication between threads, multi-threaded data sharing.

What is the main thread?

When an iOS program runs, it opens 1 threads by default, becoming the main thread or UI thread. Actions related to the UI are placed on the main thread.

The main thread is mainly in the role:

1. Display/Refresh the UI interface.

2. Handling UI Events (click, scroll, drag)

Main thread Usage Note:

Do not talk more time-consuming operations into the main line approached (such as requesting a network this operation should not be placed in the main thread)

Time-consuming operation, will be stuck in the main thread, seriously affect the UI smoothness, to give users a "card" bad experience.

Prints the current location thread number = = 1 Main thread number! = 1 Child Threads threads Other threads.

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

Putting time-consuming operations into sub-threads is a new thread space for time-consuming operations to run inside.  Longtime is the name of the method, which is the place where the child thread runs. Withobject is a parameter.

[ self Performselectorinbackground: @selector (longtime) withobject:nil];

Multi-threaded implementations in iOS:

1. Pthread: ———— programmer Management, almost without it is the underlying C language

    • A set of generic multi-threaded APIs
    • Suitable for Unix/linux/win and other systems
    • Cross-platform/portable
    • Very difficult to use

2. Nsthread programmer management occasionally using OC

    • Use more object-oriented
    • Easier to use, direct manipulation of thread objects

3.GCD Automatic management often uses C language

    • Designed to replace threading technology such as Nsthread
    • Make full use of the device's multi-core

4.NSOperation Automatic management often uses OC language

    • Based on gcd (bottom is GCD)
    • More simple and useful features than GCD.
    • Use more object-oriented

Using pthread must introduce import<pthread.h> this header file.

Thread Learning Notes

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.