Creating Threads
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.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
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 */ - in-(void) CreatNSThread2 the { the //nsthread *thread=[nsthread detachnewthreadselector: @selector (run:) totarget:self withobject:@ "Create thread Direct (auto) Start"] ; About the[Nsthread detachnewthreadselector: @selector (run:) totarget:self Withobject:@"Direct (automatic) startup of the thread after creation"]; the } the + - /** the * Nsthread Create thread mode 3Bayi * Implicit creation of threads, and direct (automatic) Start-up the */ the --(void) CreatNSThread3 - { the //execute in a background thread = = = in a child thread the[Self Performselectorinbackground: @selector (run:) Withobject:@"implicitly-created"]; the } the - the the-(void) Run: (NSString *) Str the {94 //gets the current thread theNsthread *current=[Nsthread CurrentThread]; the //Print Output the for(intI=0; i<Ten; i++) {98NSLog (@"Run---%@---%@", current,str); About } - }101 @end
iOS Development--Multithreading OC & (ii) thread creation