Multithreading in a variety of programming languages are difficult, many languages to achieve a lot of trouble, although objective-c originated from C, but its multithreaded programming is quite simple, can be comparable with Java. This article is mainly from thread creation and start, thread synchronization and lock, thread interaction, thread pool and so on four aspects of simple to explain the iphone's multithreaded programming.
First, thread creation and startup
There are two main ways to create threads:
-(ID) init; Designated initializer
-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument; there is, of course, a special kind of use of the so-called convenient method, This method can generate a thread directly and start it without being responsible for thread cleanup. The interface of this method is:
+ (void) Detachnewthreadselector: (SEL) Aselector totarget: (ID) atarget withobject: (ID) anargument after the first two methods are created, the phone needs to start, The starting method is:
-(void) start;
Synchronization and locking of threads
To illustrate the thread synchronization and lock, the best example may be multiple windows at the same time ticketing system. We know that in Java, we use synchronized to synchronize, while the iphone does not provide a Java-like synchronized keyword, but provides a Nscondition object interface. Looking at Nscondition's interface instructions, we can see that nscondition is the lock object under the iphone, so we can use nscondition to implement thread safety in the iphone. This is an example from the Web:
SellTicketsAppDelegate.h file
SellTicketsAppDelegate.h
Import
@interface Sellticketsappdelegate:nsobject {
int tickets;
int count;
nsthread* Ticketsthreadone;
nsthread* Ticketsthreadtwo;
nscondition* ticketscondition;
UIWindow *window;
}
@property (nonatomic, retain) Iboutlet UIWindow *window;
@end
SELLTICKETSAPPDELEGATE.M file
Sellticketsappdelegate.m
Import "SellTicketsAppDelegate.h"
@implementation Sellticketsappdelegate
@synthesize window;
-(void) applicationdidfinishlaunching: (uiapplication *) Application {
Tickets = 100;
Count = 0;
Lock Object
Ticketcondition = [[Nscondition alloc] init];
Ticketsthreadone = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Ticketsthreadone setname:@ "Thread-1"];
[Ticketsthreadone start];
Ticketsthreadtwo = [[Nsthread alloc] initwithtarget:self selector: @selector (run) Object:nil];
[Ticketsthreadtwo setname:@ "Thread-2"];
[Ticketsthreadtwo start];
[Nsthread detachnewthreadselector: @selector (Run) totarget:self Withobject:nil];
Override point for customization after application launch
[Window makekeyandvisible];
}
-(void) run{
while (TRUE) {
Locked
[Ticketscondition Lock];
if (Tickets > 0) {
[Nsthread sleepfortimeinterval:0.5];
Count = 100-tickets;
NSLog (@ "The current number of votes is:%d, sold:%d, thread name:%@", Tickets,count,[[nsthread CurrentThread] name));
tickets--;
}else{
Break
}
[Ticketscondition unlock];
}
}
-(void) Dealloc {
[Ticketsthreadone release];
[Ticketsthreadtwo release];
[Ticketscondition release];
[Window release];
[Super Dealloc];
}
@end
Third, the interaction of threads
Threads may need to communicate with other threads while they are running, such as modifying the interface in the main thread, and so on, using the following interface:
-(void) Performselectoronmainthread: (SEL) Aselector withobject: (ID) arg waituntildone: (BOOL) wait
Because some resources may need to be freed in this procedure, you need to use NSAutoreleasePool for management, such as:
-(void) Startthebackgroundjob {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
To does something in your thread job
...
[Self Performselectoronmainthread: @selector (makemyprogressbarmoving) Withobject:nil Waituntildone:no];
[Pool release];
}
If you don't think about it, call autorelease in the thread function, and the following error occurs:
Nsautoreleasenopool (): Object 0x********* of Class Nsconretedata autoreleased with no pool in place ....
Four, on the thread pool, you can view the relevant information nsoperation