Profileusing the Nsthread example, thread creation, startup, thread synchronization, lock, thread interaction, the thread interaction needs to be noted, because iOS rules that only the main thread can modify the UI, so if the child thread modifies the UI, it needs to interact with the main thread-the calling method -(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait; Implement
Sample code
viewcontroller.m//nsthreaddemo////Created by Wangwei on 14/12/31.//Copyright (c) 2014 Arbboter. All rights reserved.//#import "ViewController.h" @interface Viewcontroller () {nsmutablearray* sellticketthread; nscondition* sellticketcondition; Nsinteger Numberofsellthread; Nsinteger Nunberoftickets;} @end @implementation viewcontroller-(void) viewdidload{[Super Viewdidload]; Additional setup after loading the view, typically from a nib. Sellticketthread = [[Nsmutablearray alloc] init]; Numberofsellthread = 3; Nunberoftickets = 24; /** Create thread */nsthread* sellticket = nil; for (Nsinteger i=0; i<numberofsellthread; i++) {sellticket = [[Nsthread alloc] initwithtarget:self selector: @selector (Sellticket) Object:nil]; Sellticket.name = [NSString stringwithformat:@ "Ticket Windows%ld", I]; [Sellticketthread Addobject:sellticket]; }/** Create thread condition */SellticketconditIon = [[Nscondition alloc] init]; /** Start All Thread */for (nsthread* T in sellticketthread) {[t start]; } self.messageTicket.frame = CGRectMake (0, 20, 330, 600); Self.messageTicket.text = @ "Welcome to 12306.\n"; Self.messageTicket.textColor = [Uicolor blackcolor]; self.messageTicket.editable = NO;} -(void) sellticket{nsstring* message = nil; BOOL BRun = YES; while (BRun) {[sellticketcondition lock]; if (nunberoftickets>0) {nunberoftickets--; message = [NSString stringwithformat:@ "[%@] Get ticket%02ld. [%02ld left]\n], [[Nsthread Currentthr EAD] name], nunberoftickets+1, nunberoftickets]; } else {message = [NSString stringwithformat:@] [%@] Sorry, but we have no ticket lefy.\n ", [[NST Hread CurrentThread] [name]]; BRun = NO; The/** child thread cannot modify the UI of the app, so it requires the main thread to modify */[self PerformselectoroNmainthread: @selector (addmessagetoview:) withobject:message Waituntildone:yes]; [Sellticketcondition unlock]; /** wait for a moment */sleep (Arc4random ()%5+1); }}/** Interaction Information */-(void) Addmessagetoview: (nsstring*) message{self.messageTicket.text = [Self.messageTicket.text Stringbyappendingstring:message];} -(void) didreceivememorywarning{[Super didreceivememorywarning]; Dispose of any resources the can be recreated.} @end
Results show
Main Method ColumnTable
Returns the current thread + (Nsthread *) CurrentThread; Create a thread + (void) Detachnewthreadselector with a class method: (SEL) selector totarget: (ID) target withobject: (ID) argument;//to determine if it is multithreaded + (BOOL) ismultithreaded;-(nsmutabledictionary *) threaddictionary;+ (void) Sleepuntildate: (NSDate *) date;+ (void) Sleepfortimeinterval: (nstimeinterval) ti;//exit thread + (void) exit;//Thread property value + (double) threadpriority; + (BOOL) SetThreadPriority: (double) p;//thread function address + (Nsarray *) callstackreturnaddresses;//set with return thread name-(void) SetName: (NSString *) n ;-(NSString *) name;//thread Stack-(Nsuinteger) stacksize;-(void) Setstacksize: (nsuinteger) s;//determine if the current thread is the main thread-(BOOL) ismainthread;+ (BOOL) ismainthread;+ (nsthread *) mainthread;//Thread Object initialization operation (by creating a thread object, you need to manually specify the thread function with various properties)-(ID) init;//Threads object Create a thread at initialization (specify thread function)-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument;//is executing-(BOOL) Whether the isexecuting;//has ended-(bool) isfinished;//canceled-(bool) iscancelled;//cancel operation-(void) cancel;//executes the code for Aselector on the main thread, Because some code can only be executed on the main thread, such as modifying ui-(void) PerformselectoronmaiNthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait;//thread Start-(void) start;-(void) main; Thread Body method
Nsthread of multi-threaded development in iOS