iOS Development Threading Summary--nsthread

Source: Internet
Author: User

1, Introduction: 1.1 iOS has three kinds of multi-threaded programming technology, respectively:

1., Nsthread

2. Cocoa nsoperation (use of nsoperation and Nsoperationqueue for iOS multithreaded programming)

3. GCD Full Name: Grand Central Dispatch (Introduction and use of the Grand Central Dispatch (GCD) for iOS multithreaded programming

These three kinds of programming from top to bottom level of abstraction from low to high, the higher the abstraction of the more simple to use, is also the most recommended for Apple.

In this article we mainly introduce and use Nsthread, the following will continue to 2, 3 of the explanation and use.

1.2 Three ways of having disadvantages are introduced:

Nsthread:

Pros: Nsthread than the other two lightweight

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

Nsthread is created in two ways:

You can see from the following code that Nsthread is concurrent synchronous execution

- (void) viewdidload {[Super viewdidload];    [Nsthread detachnewthreadselector: @selector (myThread) totarget:self Withobject:nil]; NSLog (@"234");}-(void) mythread{Sleep (5); NSLog (@"123");}2.- (void) viewdidload {[Super viewdidload]; Nsthread* Mathread = [[Nsthread alloc] initwithtarget:self selector: @selector (MyThread)Object: nil];    [Mathread start]; NSLog (@"234");}-(void) mythread{Sleep (5); NSLog (@"123");}

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

2.4.2 Inter-thread communication

How to notify the main thread update interface after downloading the picture?

[self performselectoronmainthread:@selector(updateUI:) withobject: Image Waituntildone :YES];

Performselectoronmainthread is the NSObject method, in addition to updating the main thread's data, you can also update other threads such as:

By: PerformSelector:onThread:withObject:waitUntilDone:

iOS Development Threading Summary--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.