OBJECTIVE-C Development-Multi-threaded (Nsthread)

Source: Internet
Author: User

What is multithreading?

First, what is a thread that you can understand as a thread is the process process.

Process is God horse?

A process is a small task in a computer.

Let me tell you this.

Our main program is a rib fool, only one thing in a time. Also, all programs that manipulate UI controls can only be done by the main thread.

But, for example, accessing the network is a time-consuming, laborious, and potentially dead thing, and if placed in the main program, the user experience is too bad,

Because, this thing is not finished, the back of the matter will not be able to do.

The main program is like this, one thing is done, then another thing. It's not done, just wait.

So the user is waiting here? You have a progress bar is OK, if even the progress bar is not, I, the user thought the crash.

Of course we have a solution-multi-Threading.

Multithreading is not just about OC technology, JAVA,. NET has the relevant solution, but the grammatical framework is different.

Today, the protagonist is OC, we will look at how OC is dealing with multithreading.

This is certainly not what I summed up, take it for a moment.

The first one we will not say, almost do not use of things, I will not, we start from Nsthread.

Here is a demo, just a Viewcontroller, click on the screen to run the blank

//

Viewcontroller.m

Nsthreadtest

//

Created by Tarena on 16/3/28.

copyright©2016 year Tarena. All rights reserved.

//

#import "ViewController.h"

@interface viewcontroller ()

@end

@implementation Viewcontroller

-(void) Touchesbegan: (nsset<uitouch *> *) touches withevent: (uievent *) event{

// mode one:

//object parameters for the child threading method

nsthread *thread1 = [[nsthread alloc]initwithtarget:self Selector :@selector(method:) Object:@ " mode one start "];

// need to start up a bit

[Thread1 start];

// mode two:

// auto-start

[nsthread detachnewthreadselector:@selector(method:) totarget:self withobject:@ " mode two start "];

// Way three:

[self performselectorinbackground:@selector(method:) withobject:@ " mode three start "];

}

-(void) method: (nsstring*) name{

for (int i=0; i<; i++) {

NSLog(@ " I am %@,i=%d", name,i);

}

}

-(void) viewdidload {

[Super viewdidload];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

//Dispose of any resources, can be recreated.

}

@end

Here is the console section, see, sub-threads are running, the main thread is not a bit of cards.

OBJECTIVE-C Development-Multi-threaded (Nsthread)

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.