IOS Multi-threaded learning Notes

Source: Internet
Author: User

This article copies, the reference from the article: iOS multithreading programming nsthread use, mainly in order to strengthen the individual to the knowledge understanding and the memory, does not do his use. Thanks for the hard work of the original author here!

1. Introduction

1.1 iOS Multi-threaded programming technology classification

(1) Nsthread

(2) Cocoa nsoperation

(3) GCD (Grand Central Dispatch)

These three ways from top to bottom, the level of abstraction gradually increased, the use of more and more simple.

1.2 Three ways of pros and cons

Advantages Disadvantages
Nsthread Light weight You need to manage the thread's life cycle, thread synchronization. When thread synchronization is locked, there is a certain amount of system overhead.
Nsoperation No need to care about thread management, data synchronization, you can focus on the actions you need to perform
GCD iOS4.0, to replace the nsthread,nsoperation and other technologies, very efficient, powerful

2. Use of Nsthread

2.1 How to create

(1) Instance method creation

-(ID) Initwithtarget: (IDObject:(ID) argument
Example: Nsthread* myThread = [[Nsthread alloc] initwithtarget:self selector: @selector (dosomething:) Object: nil]; [MyThread start];

(2) Nsthread class method creation

+ (void) Detachnewthreadselector: (SEL) aselector totarget: (ID) atarget withobject: (ID  anargument example: [Nsthread detachnewthreadselector: @selector (dosomething:) totarget:self Withobject:nil];

(3) NSObject non-display method creation

+ (void) Performselectorinbackground: (SEL) aselector withobject: (ID) anargument example [OBJ Performselectorinbackground: @selector (dosomething:) Withobject:nil];

Selector: Thread execution method, this selector can only have one parameter, and cannot have the return value;

Target:selector object sent by message

Argument: The only parameter transmitted to selector, or nil

The first way is to create the thread directly and start running the thread, and the second method is to create the thread object before running the thread, and then you can set the thread's priority and so on thread information before running the thread operation.

IOS Multi-threaded 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.