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) implicitly creating and starting threads
[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.M3 //4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 Ten #import "YYViewController.h" One #import<pthread.h> A - - @interfaceYyviewcontroller () the-(ibaction) Btnclick; - @end - - + @implementationYyviewcontroller - + A- (void) Viewdidload at { - [Super Viewdidload]; - } - - - //button's Click event in-(ibaction) Btnclick { - //1. Get the current thread toNsthread *current=[Nsthread CurrentThread]; + //Main Thread -NSLog (@"btnclick----%@", current); the * //2. Use a For loop to perform some time-consuming operations $ pthread_t thread;Panax NotoginsengPthread_create (&thread, NULL, run, NULL); - } the + A //C language Functions the void*run (void*data) + { - //gets the current thread, which is the newly created thread $Nsthread *current=[Nsthread CurrentThread]; $ - - for(intI=0; i<10000; i++) { theNSLog (@"Btnclick---%d---%@", i,current); - }Wuyi returnNULL; the } - Wu //multiple threads, the main thread is not blocked when you click the button to execute the button call method - About @end
Implementation results:
Printing results:
2. Create a thread using Nsthread
1 //2 //YYVIEWCONTROLLER.M3 //4 //5 //Created by Apple on 14-6-23.6 //Copyright (c) 2014 itcase. All rights reserved.7 //8 9 #import "YYViewController.h"Ten #import<pthread.h> One A - @interfaceYyviewcontroller () --(ibaction) Btnclick; the @end - - - @implementationYyviewcontroller + -- (void) Viewdidload + { A [Super Viewdidload]; at } - - - //button's Click event --(ibaction) Btnclick { - //1. Get the current thread inNsthread *current=[Nsthread CurrentThread]; - //Main Thread toNSLog (@"btnclick----%@", current); + - //another way to get the main thread theNsthread *main=[Nsthread Mainthread]; *NSLog (@"main thread-------%@", main); $ Panax Notoginseng //2. Take some time-consuming actions - [self creatnsthread]; the //[self creatNSThread2]; + //[self creatNSThread3]; A } the + - /** $ * Nsthread Create thread mode 1 $ * 1> Create initialization thread first - * 2> start open thread - */ the-(void) Creatnsthread - {WuyiNsthread *thread=[[nsthread alloc] initwithtarget:self selector: @selector (run:)Object:@"Thread A"]; the //set a name for a thread -Thread.name=@"Thread A"; Wu //Open Thread - [Thread start]; About $ -Nsthread *thread2=[[nsthread alloc] initwithtarget:self selector: @selector (run:)Object:@"thread B"]; - //set a name for a thread -Thread2.name=@"thread B"; A //Open Thread + [thread2 start]; the } - $ the /** the * Nsthread Create thread mode 2 the * Create thread Direct (auto) Start the */ --(void) CreatNSThread2 in { the //nsthread *thread=[nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Create thread Direct (auto) Start"] ; the About[Nsthread detachnewthreadselector: @selector (run:) totarget:self Withobject:@"Direct (automatic) startup of the thread after creation"]; the } the the + /** - * Nsthread Create thread mode 3 the * Implicit creation of threads, and direct (automatic) Start-upBayi */ the-(void) CreatNSThread3 the { - //execute in a background thread = = = in a child thread -[Self Performselectorinbackground: @selector (run:) Withobject:@"implicitly-created"]; the } the the the --(void) Run: (NSString *) Str the { the //gets the current thread theNsthread *current=[Nsthread CurrentThread];94 //Print Output the for(intI=0; i<Ten; i++) { theNSLog (@"Run---%@---%@", current,str); the }98 } About @end
Call Thread 1 and print the result:
Calling thread 2
Calling thread 3
iOS Development multithreaded article-Creating threads