Oh, try to break it down into a section of the writing. It's hard for everyone to see clearly. I have three ways to create threads here. The code is as follows
#import "ViewController.h"
@interface viewcontroller ()
@end
@implementation Viewcontroller
-(void) viewdidload {
[superviewdidload];
}
When the user taps the screen, the thread executes
-(void) Touchesbegan: ( Nsset *) touches withevent: (uievent *) Event
{
[self testthread];
}
-(void) testthread
{
// fetch to the current thread , Here is the main path
nsthread *curthread = [nsthreadcurrentthread ];
NSLog(@ "curthread =%@", curthread);
// // method to fetch the main thread
Nsthread *mainthread = [Nsthread mainthread];
NSLog (@ "mainthread =%@", mainthread);
//
[selfcreateThread1];
}
-(void) createThread1
{
// a nsthread object is a thread
// parameter 1,2: specified in thread by parameter 1 method of calling parameter 2
// parameter 3: pass an argument to the method specified by parameter 2
nsthread *thread = [[nsthreadalloc] Initwithtarget:selfselector:@selector( ThreadMain:)object:@ " thread creation mode 1"];
[Thread setName:@ " My name is two eggs "];
[Thread start];
}
-(void) createThread2
{
[nsthreaddetachnewthreadselector:@selector (threadmain:)totarget:selfwithobject: @ " thread creation mode 2"];
}
-(void) createThread3
{
[selfperformselectorinbackground:@selector (threadmain:)withobject:@ " thread creation mode 3"];
}
-(void) ThreadMain: ( ID ) obj
{
for (int i =0; i<; i+ +) {
NSLog(@ "i =%d,obj =%@,thread =%@", i,obj,[nsthread currentthread]);
}
}
Of course, we still need to use more, like eating, eat more can be picky, continue the next article, this evening 3 more. Ha ha
IOSGCD multi-thread creation multithreading