IOS-----Multi-thread Nsthread

Source: Internet
Author: User

Multithreading

The iOS platform provides excellent multithreading support, and the program can be launched in a very simple way, and the iOS platform not only provides the Nsthread class to create multi-threading, but also provides a GCD way to simplify multithreaded programming. Provides nsoperation and Nsoperationqueue support for multithreaded programming. Overall, iOS has tried to reduce the complexity of developing multithreaded applications, making it easy and easy for developers to develop multithreaded applications.

Threading Overview

Almost all operating systems support running multiple tasks at the same time, and a task is usually a program, and each running program is a process. When a program runs, the internal may contain multiple sequential execution flows, and each sequential execution flow is a thread.

threads, tasks, processes

Threads and Processes

All running tasks usually correspond to a process, and when a program goes into memory, it becomes a process, a process that is in the process of running, and has a certain independent function, and the process is an independent unit of the system's resource allocation and scheduling.

The process consists of the following 3 characteristics

Independence

A process is a separate entity in a system that can have its own independent resources and each process has its own private address space. A user process does not have direct access to the address space of another process without the process itself permitting.

Dynamic nature

The difference between a process and a program is that a program is just a static set of instructions, and a process is a collection of instructions that are active in the system. The concept of time is added to the process, which has its own life cycle and a variety of different states, which are not available in the program.

Concurrency of

Multiple processes can execute concurrently on a single processor, with no interaction between multiple threads.

Note

Meaning

concurrency (concurrency) and parallelism (parallel) are two concepts,

Parallel means that at the same time, multiple instructions are executed simultaneously on multiple processors;

Concurrency means that only one instruction can be executed at the same time, but multiple process instructions are executed quickly, making it possible to have multiple processes concurrently executing at the macro level.

Thread

Multithreading enables the same process to handle multiple tasks concurrently simultaneously. Threads are also known as lightweight processes (lightweight process), which are the execution units of a process. A thread is a separate, concurrent execution flow in a program. When the process is initialized, the main thread is created. For most applications, there is usually only one main thread required, but we can also create multiple sequential execution flows within that process, which are threads and each thread is independent of each other.

A thread is part of a process, a process can have multiple threads, and a thread must have a parent process. Threads can have their own stacks, their own program counters, and their own local variables, but no longer have system resources that share all the resources owned by the process with other threads of the parent process. Programming is easier because multiple threads share all the resources in the parent process, but you must be more careful and we must ensure that threads do not interfere with other threads in the same process

In short: After a program runs at least one process, a process can contain multiple threads, but at least one thread. The operating system can perform multiple tasks at the same time, each of which is a process, and the process can perform multiple tasks at the same time, each of which is a thread.

Multi-Threading Benefits

Threads are independent, concurrent execution flows in a program, and are less isolated from threads in a process than separated processes. They share memory, file handles, and other status applied by each process. Threads in the same process have commonalities----multiple threads will share the same process virtual space. Thread-shared environments include: Process code snippets, process public data, and so on. With these shared data, it is easy for threads to communicate with each other.

Advantages of multithreading

1

Memory cannot be shared between processes, but it is easy to share memory between threads.

2

The system creation process needs to reallocate system resources for the process, but creating threads is much less expensive, so using multithreading to achieve multitasking is more efficient than multiple processes.

3

iOS simplifies multithreaded programming for iOS by providing multiple ways to implement multithreading.

iOS roughly provides the following 3 types of multithreaded programming techniques

1

Multithreading is implemented using Nsthread.

2

Multithreading with Nsoperation and Nsoperationqueue

3

Using GCD (Grand Central Dispatch) for multithreading

using Nsthread for multithreading

iOS uses the Nsthread class to represent threads, creating new threads that are creating Nsthread objects.

Creating and starting Threads

There are two ways of creating nsthread.

-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) arg

Create a new Thread object

+ (void) Detachnewthreadselector: (SEL) Selector object: (ID) target withobject: (ID) arg

Create and start a new thread

+ CurrentThread:

CurrentThread is a class method of the Nsthread class that always returns the currently executing thread object

SetName:

The name of the specified thread can be returned by the name method

The essence of both approaches is to convert the selector method of the target object to the thread execution body, where the selector method can receive a maximum of one parameter, and ARG represents the argument passed to the selector method.

Mention

Shown

The method body of the target object's Selector method represents the task that the thread needs to accomplish, so it is equivalent to converting the target object's selector method to the thread execution body.

Area

Don't

The 1th method is an instance method that returns a Nsthread object that must be called by the Start method to launch the thread

The 2nd way does not return the Nsthread object, so this way directly creates and starts the thread.

the state of the thread

The start thread uses the Start method, and after the thread is started it is not immediately in the running state, the thread is ready after it is started, and the thread goes into a running state when the system adjusts the thread.

If the program wants to invoke the child thread's Start method after the child thread immediately begins execution, the program can use [Nsthread sleepfortimeinterval:0.001];

Let the currently running thread (the main thread) sleep 1 milliseconds---1 milliseconds is enough because the CPU will not be idle for 1 milliseconds, it will execute another thread in the ready state, so that the child thread can be executed immediately.

terminating a child thread

The thread ends in one of 3 ways, and then dies at the end.

The thread execution method execution completes and the thread ends normally.

Error occurred during thread execution

Call the Exit method of the Nsthread class directly to abort the currently executing thread.

Attention

When the main thread ends, the other threads are unaffected and will not end. Once the child thread starts up, it has the same status as the main thread, and it is not affected by the main thread.

To test whether a thread is running, you can call the thread object's isexcuting, isfinished method, and when the thread is in execution, the call to the Isexecuting method returns Yes, and when the thread finishes executing, Calling the Isfinished method returns Yes.

The ① code in the above program calls the Cancel method of the thread object, which is used to send a cancellation signal to the thread object, which makes the iscancelled method of the Thread object

Returns YES------the next program thread execution method line to determine whether thread object D iscancelled is yes, if the object's iscancelled is yes, the program calls the thread class's exit method to terminate the currently executing loop, This makes it possible to terminate the child thread from the UI thread.

Thread Sleep

The currently executing thread needs to be paused for a period of time and into a blocking state, by invoking the static of the Nsthread class. Sleepxxx method to complete.

The Nsthread class provides the following two class methods for controlling thread pauses

+ (void) Sleepuntildate: (NSDate *) Adate:

Lets the currently executing thread pause to the time represented by the adate and into a blocking state

+ (void) Sleepfortimeinterval: (nstimeinterval) Ti:

Let the currently executing thread pause ti seconds and go into a blocking state.

When a thread calls the Sleepxxx method into a blocking state, during its sleep time period, it does not get an opportunity to execute, even if there are no other threads executing in the system, the blocked thread will not execute, so the Sleepxxx method is often used to pause the execution of the thread.

[Nsthread sleepfortimeinterval:0.5];

Used to control the currently executing thread is paused for 0.5 seconds

IOS-----Multi-thread Nsthread

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.