A preliminary study of multi-threading in iOS (i)--nsthread

Source: Internet
Author: User

A preliminary study of multi-threading in iOS (i)--nsthread

For multi-threaded development, The iOS system provides a number of different interfaces, first of all, the use of the most basic aspects of iOS multithreading. There are two kinds of ways to generate threads, one for explicit invocation and the other for implicit invocation.

First, the class that shows the call is nsthread. There are two ways to construct a Nsthread thread object in general:

1. Initialize the thread Master method:

[Nsthread detachnewthreadselector: @selector (run:) totarget:target withobject:obj]; Class method

Or

nsthread *newthread = [[Nsthread alloc] initwithtarget:target selector: @selector (run:) object:obj]; // The instance method can get the thread object, which makes it easier to terminate the thread later.

2. Define the subclass Mythread of the Nsthread, and then implement the Main method (that is, run in Method 1). Then create a new object:

MyThread *newthread = [[MyThread alloc] init];

Start thread: [Newthread start];

Terminating thread: There is actually no API to really provide a terminating thread, but there is a cancel method available; It changes a status flag that the thread runs, and we can take advantage of this:

The thread loop is implemented in the run: or Main method first:

-(void) Main

{

Thread Init

while (![ [nsthread currentthread] iscancelled])

{

Thread loop

[nsthread sleepfortimeinterval:1.0];//equivalent to sleep (1);

}

Release resources of Thread

}

At this point if [Newthread Cancel] is called; You can terminate the thread loop.

Nsthread has a class method exit is used to immediately end the running of the current thread (unsafe), because the current thread cannot be guaranteed to release the resource, so it is not recommended. The Stop method, like thread in Java, is also deprecated because it is unsafe.

Second, implicit invocation

By NSObject the category method call, listed as follows:

-(void) Performselectoronmainthread: (SEL) aselector withobject: (ID) arg waituntildone: (BOOL) wait; Running the method in the main thread, wait indicates whether to block the call of this method, and if yes, waits for the end of the running method in the main thread. It is generally useful to invoke a UI method in a child thread.

-(void) Performselector: (SEL) aselector onthread: (Nsthread *) thr Withobject: (ID) arg waituntildone: ( BOOL) wait; Executes in the specified thread, but the thread must have a run loop.

-(void) Performselectorinbackground: (SEL) aselector withobject: (ID) arg; implies the creation of a new thread.


Third, some of the other common methods of Nsthread

The thread created is a non-associative thread (detached threads), where the parent and child threads do not have a dependency, and the parent thread ends does not mean that the child thread ends.

1. + (nsthread *) CurrentThread; //Get current thread

2. + (void) Sleepfortimeinterval: (nstimeinterval) ti; Thread hibernation

3. + (Nsthread *) mainthread; //main thread, i.e. UI thread

4. -(bool) Ismainthread; + (bool) ismainthread; //When the front thread is the main thread

5. -(BOOL) isexecuting; //thread is running

6. -(BOOL) isfinished; //Whether the thread has ended

Iv. Some non-threaded calls (the NSObject category method)

This is done on the current thread, noting that they block the current thread (including the UI thread):

-(ID) performselector: (SEL) aselector;

-(ID) performselector: (SEL) aselector withobject: (ID) object;

-(ID) performselector: (SEL) aselector withobject: (ID) object1 withobject: (id) object2;

To downgrade the current thread for deferred execution, if the current thread is not explicitly using Nsrunloop or has exited to execute, you need to be aware of this:

-(void) Performselector: (SEL) aselector withobject: (ID) anargument afterdelay: (nstimeinterval) delay Inmodes: (Nsarray *) modes;

-(void) Performselector: (SEL) aselector withobject: (ID) anargument afterdelay: (nstimeinterval) Delay;

And they can be terminated:

+ (void) Cancelpreviousperformrequestswithtarget: (ID) atarget selector: (SEL) Aselector object: ( ID) anargument;

+ (void) Cancelpreviousperformrequestswithtarget: (id) atarget;

V. Sequence of thread execution

Typically, when the UI needs to display network data, you can simply take advantage of the thread's execution order to avoid explicit thread synchronization:

1. UI thread Invocation

[Threadobj performselectorinbackground:@selector(loaddata) withobject:nil];

2. Callback UI thread in child thread to update UI

-(void) LoadData

{

Query data from Network

Update Data Model

Callback UI Thread

[Uiobj performselectoronmainthread:@selector(UpdateUI) withobject:nil waituntildone: YES];

}

You can also use Nsthread to achieve the same functionality, LoadData equivalent to Nsthread's main method.

A preliminary study of multi-threading in iOS (i)--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.