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)