There are 3 common multithreaded programming methods in iOS

Source: Internet
Author: User

1.NSThread

This approach requires management of the thread's lifecycle, synchronization, and locking issues, resulting in a certain performance overhead

2.NSOperation and the Nsoperationqueue

is based on OC implementation. Nsoperation encapsulates the actions that need to be performed in an object-oriented manner, and can then put this operation into a nsoperationqueue to execute asynchronously. Do not care about thread management, synchronization, and so on.

3.Grand centeral Dispatch

Short GCD,IOS4 only started to support, is the pure C language API. Since IPAD2 began, the Apple device has a dual-core CPU, in order to take full advantage of these 2 cores, GCD provides some new features to support multi-core parallel programming

A Nsthread instance represents a thread

One, gets the current thread

Nsthread *current = [Nsthread CurrentThread];

Second, get the main thread

1 Nsthread *main = [Nsthread mainthread];

2 NSLog (@ "Main thread:%@", main);

The printing results are:

2013-04-18 21:36:38.599 thread[7499:c07] Main thread: <nsthread:0x71434e0>{name = (null), num = 1}

Num equals the ID of the thread, and num for the main thread is 1.

Third, Nsthread the Creation

1. Dynamic Methods

-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument;

* A new thread is created on line 2nd, and then the Start method is called on line 4th, and the thread starts with the run: Method of self and calls @ "MJ" as the method parameter

1//Initialize thread

2 Nsthread *thread = [[[Nsthread alloc] initwithtarget:self selector: @selector (run:) object:@ "MJ"] autorelease];

3//Open thread

4 [Thread start];

If run: The method is this:

1-(void) Run: (NSString *) string {

2 Nsthread *current = [Nsthread CurrentThread];

3 NSLog (@ "Run: Method-parameter:%@, current thread:%@", string, present);

4}

Printing results are:

2013-04-18 21:40:33.102 Thread[7542:3e13] Executed run: Method-parameter: MJ, current thread: <nsthread:0x889e8d0>{name = (null), num = 3}

It can be found that the NUM value of this thread is 3, indicating that it is not the main thread, and num of the main thread is 1

2. static Methods

+ (void) Detachnewthreadselector: (SEL) selector totarget: (ID) target withobject: (id) argument;

[Nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "MJ"];

After executing the above code, a new thread is started and the Self's Run: method is called on this thread, with @ "MJ" as the method parameter

3. implicitly creating Threads

[Self Performselectorinbackground: @selector (run:) withobject:@ "MJ"];

Creates a new thread implicitly, and calls Self's run: method on this thread, with @ "MJ" as the method parameter

Iv. pausing the current thread

[Nsthread Sleepfortimeinterval:2];

NSDate *date = [nsdate datewithtimeinterval:2 sincedate:[nsdate Date]];

[Nsthread Sleepuntildate:date];

Both of the above are pausing the current thread for 2 seconds

v. Other operations of the thread

1. perform actions on the specified thread

1 [Self performselector: @selector (run) onthread:thread Withobject:nil Waituntildone:yes];

* The above code means that the run method of self is called on thread

* The last yes means: The code above will block, and when the Run method finishes executing the thread, the code above will pass

2. perform actions on the main thread

[Self performselectoronmainthread: @selector (Run) Withobject:nil Waituntildone:yes];

Call the Run method of self on the main thread

3. performing actions on the current thread

[Self performselector: @selector (Run) Withobject:nil];

Call the Run method of self on the current thread

Vi. Advantages and disadvantages

1. Advantages: Nsthread is lighter than other multithreaded schemes and controls threading objects more intuitively

2. Cons: You need to manage the thread's life cycle, thread synchronization. Thread synchronization has a certain overhead in locking data

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.