Differences between single-thread and multi-thread, single-thread, and multi-thread

Source: Internet
Author: User

Differences between single-thread and multi-thread, single-thread, and multi-thread

Programs, processes, threads

Program: executable Application Generated by source code. (For example, QQ. app)

Process: A running program can be considered as a process (for example, a running QQ alarm once). The process has all the resources required for independent operation.

Thread: A code segment that runs independently in a program. (For example, the Code for receiving QQ messages)

A process is composed of one or more threads. A process is only responsible for resource scheduling and allocation. A thread is the real execution unit of the program and is responsible for code execution.

 

 

Single thread

Each running program (that is, a process) includes at least one thread, which is called the main thread.

The main thread is created when the program is started and used to execute the main function.

A program with only one main thread, called a single-threaded Program

The main thread is responsible for executing all the code of the Program (UI display and refresh, network requests, local storage, etc ). These codes can only be executed sequentially and cannot be executed concurrently.

 

Multithreading

A program with multiple threads is called a multi-threaded program.

IOS allows users to open up new threads themselves. These threads are called subthreads as opposed to the main threads.

Several sub-threads can be opened as needed

The sub-thread and main thread are independent operation units, and their respective operations do not affect each other. Therefore, they can be executed concurrently.

 

Differences between single thread and multi-thread

Single-threaded program: Only one thread executes the code in sequence and is prone to code blocking (the page is suspended)

Multi-threaded program: multiple threads run independently between threads, which can effectively avoid code blocking and improve program running performance.

Note: In iOS, you must add and refresh the UI in the main thread.

 

 

Multithreading on iOS platform

 

IOS multithreading implementation types

NSThread

NSOperationQueue

NSObject

GCD

 

NSThread

NSThread is a lightweight multi-threaded program. It can be created using either of the following methods:

Note:

In the multi-threaded method, you need to add an automatic release pool.

When an application is opened, the system automatically creates an automatic release pool for the main thread.

The sub-thread we manually created requires us to manually add the Auto Release pool.

 

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.