I. Simple instructions for creating and starting a thread
A Nsthread object represents a thread
Creating, starting Threads
(1) nsthread *thread = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Thread start];
When a thread is started, the run method of self is executed in threads thread
Main thread related usage
+ (Nsthread *) Mainthread; Get the main thread
-(BOOL) Ismainthread; is the main thread
+ (BOOL) Ismainthread; is the main thread
Other usage
Get current thread
Nsthread *current = [Nsthread CurrentThread];
Scheduling priority for a thread: The value of the scheduling priority is 0.0 ~ 1.0, the default is 0.5, the higher the value, the greater the priority
+ (double) threadpriority;
+ (BOOL) SetThreadPriority: (double) p;
Set the name of the thread
-(void) SetName: (NSString *) n;
-(NSString *) name;
Other ways to create threads
(2) automatically start thread after creating thread [Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
(3) Create and start the thread implicitly [self Performselectorinbackground: @selector (Run) Withobject:nil];
Pros and cons of the above 2 ways to create threads
Advantages: Simple and fast
Cons: Unable to set the thread in more detail
Second, code example
1. Create it in an ancient way
1//2//YYVIEWCONTROLLER.M 3//4//5//Created by Apple on 14-6-23.6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import <pthread.h>12 @interface Yyviewcontroller (ibaction ) btnclick;16 @end17 @implementation YYViewController20-(void) viewDidLoad23 {[Super viewdidload];25 }26 27 28//button click event (ibaction) Btnclick {30//1. Get current thread Nsthread *current=[nsthread currentthread];32//Master Thread NSLog (@ "Btnclick----%@", current); 34 35//2. Use the For loop to perform some time-consuming operations. pthread_t thread;37 pthread_create (&thread, NULL, run, NULL);}39 all//c language function *run void (void *data) 43 {44//Gets the current thread, is the newly created thread Nsthread *current=[nsthread currentthread];46 (int i=0; i<10000; i++) {NSLog (@ "Btnclick---%d---%@", i,current),}51 return null;52}53 54//Multiple threads, click the button to execute the button call method, the main thread is not blocked 55 @end57 (+)
Implementation results:
Printing results:
2. Create a thread using Nsthread
1//2//YYVIEWCONTROLLER.M 3//4//5//Created by Apple on 14-6-23. 6//Copyright (c) 2014 itcase. All rights reserved. 7//8 9 #import "YYViewController.h" #import <pthread.h> @interface Yyviewcontroller ()-(IBAc tion) Btnclick; @end @implementation Yyviewcontroller-(void) viewdidload {[Super viewdidload]; 23} 24 25 26//button click event (ibaction) Btnclick {28//1. Get current thread nsthread *current=[nsthread CurrentThread]; 30//Master Thread NSLog (@ "Btnclick----%@", current); 32 33//Get the main thread another way Nsthread *main=[nsthread Mainthread]; NSLog (@ "main thread-------%@", main); 36 37//2. Perform some time-consuming operations [self creatnsthread]; //[self creatNSThread2]; //[self creatNSThread3]; /** * Nsthread Create thread mode 1 * 1> first create initialization thread * 2> start open Thread */-(void) Creatnsthread 50 { Wuyi nsthread *thread=[[nsthread alloc]initwithtarget:self selector: @selector (run:) object:@ "Thread A"]; 52//Set a name for the thread: [email protected] "Thread A"; 54//Open threads [Thread start]; Nsthread *thread2=[[nsthread alloc]initwithtarget:self selector: @selector (run:) object:@ "thread B"]; 59/ /set a name for the thread [email protected] "thread B"; 61//Open Thread [thread2 start]; /** * Nsthread Create thread mode 2 68 * Direct (Auto) start of thread creation (automatic) (void) creatNSThread2//Nsthread *t Hread=[nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Create thread Direct (auto) start"]; [Nsthread D Etachnewthreadselector: @selector (run:) totarget:self withobject:@ "Create thread Direct (auto) start"]; /** * Nsthread Create thread mode 3 81 * Implicitly creates thread and directly (automatically) starts a. */-(void) CREATNSTHREAD3 85 {86//in background thread = = = Executes in a child thread [self performselectorinbackground: @selector (run:) withobject:@ "implicitly created"]; the "the" (void) Run :(NSString *) STR 93 {94//Get current thread nsthread *current=[nsthread CurrentThread]; 96//print output (int i= 0; i<10; i++) {98 NSLog (@ "Run---%@---%@", current,str),}100}101 @end
Call Thread 1 and print the result:
Calling thread 2
Calling thread 3