IOS: 41 multithreading and ios41 Multithreading

Source: Internet
Author: User

IOS: 41 multithreading and ios41 Multithreading
1. multithreading Overview 1> concepts of programs, processes, and processes

  • 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 is a process), and 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. The process is only responsible for resource scheduling and allocation. The thread is the real execution unit of the program and is responsible for code execution.

2> single thread
  • Each running program (process) contains 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 is called a single-threaded program.

  • In 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.

3> 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, so they can be executed concurrently.

4> Differences between single-thread and multi-thread
  • Single-threaded program: there is only one thread, that is, the main thread, code execution in sequence, 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.

2. Create multiple threads for NSThread and NSObject. 1> Create NSThread

  

  • NSThread
// Parameter 1: target // parameter 2: Method // parameter 3: passed parameter NSThread * thread = [[NSThread alloc] initWithTarget: self selector: @ selector (sayHi) object: nil]; // enable the thread [thread start];
  • NSThread automatically opens sub-threads
// The thread automatically starts [NSThread detachNewThreadSelector: @ selector (sayHi) toTarget: self withObject: nil];
2> other common NSThread attributes and Methods

Declaration of other common attributes and methods in the header file:

// Obtain the current thread + (NSThread *) currentThread; // The Master thread sleeps ti seconds + (void) sleepForTimeInterval :( NSTimeInterval) ti; // directly exit the thread + (void) exit; // determine whether the main thread + (BOOL) isMainThread; // obtain the main thread + (NSThread *) mainThread; // cancel the thread, not the real cancellation, it only sends a signal to the thread and cancels-(void) cancel through this signal; // start the thread-(void) start when the sub-thread is opened manually;

Code:

// Obtain the NSLog of the current thread (@ "currentThread =%@", [NSThread currentThread]); // obtain the NSLog of the main thread (@ "mainThread =% @", [NSThread mainThread]); // determines whether the current thread is the main thread NSLog (@ "isMainThread = % d", [NSThread isMainThread]);
3> enable NSObject sub-Thread
-(Void) viewDidLoad {[super viewDidLoad]; // use javasmselectorinbackground to open up sub-threads. // parameter 1: selector // parameter 2: parameter passed by the method [self defined mselectorinbackground: @ selector (sayHi) withObject: nil]; self. view. backgroundColor = [UIColor yellowColor];} // subthread execution method-(void) sayHi {NSLog (@ "Hello World"); NSLog (@ "currentThread = % @", [NSThread currentThread]); NSLog (@ "mainThread =%@", [NSThread mainThread]); // return to the main thread to modify the current background color. // parameter 1: selector // parameter 2: passed parameter // parameter 3: whether to enter the main thread after the sub-thread completes execution [self defined mselecw.mainthread: @ selector (mainThreadChangeColor) withObject: nil waitUntilDone: YES];} // main thread execution method-(void) mainThreadChangeColor {self. view. backgroundColor = [UIColor cyanColor]; NSLog (@ "currentThread = % @", [NSThread currentThread]); NSLog (@ "mainThread = % @", [NSThread mainThread]);}
4> end of Thread

The opening thread implemented by NSThread and NSObject will be automatically released by the system.

There are two ways to end a thread:

// Cancel the thread [thread cancel]; // It is not a real cancellation, but a signal is sent to the thread to cancel it. // The thread is directly exited [NSThread exit];
3. NSOperationQueue 1> Overview
  • NSOperation class, which belongs to M in MVC and is an abstract class used to encapsulate code and data related to a single task.

  • It is abstract and cannot be used directly. Instead, it uses a subclass (NSInvocationOperation or NSBlockOperation) to execute the actual task.

  • NSOperation (including subclass) is only an operation. It has no main thread or subthread, and can be used in any thread. It is usually used in combination with NSOperationQueue.

2> NSInvocationOperation
  • NSInvocationOperation is a subclass of NSOperation.

  • The target and action to be executed are encapsulated.

NSInvocationOperation * invocationOperation = [[NSInvocationOperation alloc] initWithTarget: self selector: @ selector (test) object: nil]; // when using NSOperation subclass to create a thread separately, [invocationOperation start];-(void) test {NSLog (@"

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.