The application of threads in the iphone is not uncontrolled, and the official data shows that the main thread of the iphone OS has a stack size of 1M and the second one starts at 512KB. And the value cannot be changed by the compiler switch or the thread API function.
Only the main thread has the ability to directly modify the UI.
First, nsoperation and Nsoperationqueue
1, an Operation class inherited from Nsoperation, the implementation of the class must have-(void) The Main method.
2, the simplest way to use nsoperation is to put it into the nsoperationqueue.
Once an operation is queued, the queue starts and processes it (that is, the main method that invokes the action Class). Once the operation completes the queue, it is freed.
Self. Queue = [[Nsoperationqueue alloc] init];
Articleparseoperation *parser = [[articleparseoperation alloc] initwithdata: FilePath Delegate:self];
[queue addoperation:parser];
[parser release];
[queue release];
3, you can set the operation queue up to the number of colleagues running operations: [Queue Setmaxconcurrentoperationcount:2];
Second, nsthread< transfer >
One, thread creation and startup
There are two main ways in which thread creation works:
-(ID) init;//designated initializer-(ID) Initwithtarget: (ID) Target selector: (SEL) Selector object: (ID) argument;
Of course, there is one more special, that is, using the so-called convenient method, which can generate a thread directly and start it without being responsible for the cleanup of the thread. The interface for this method is :
+ (void) Detachnewthreadselector: (SEL) Aselector totarget: (ID) atarget withobject: (ID) anargument
After the first two methods are created, the phone needs to be started and the method to start is:
-(void) start;
Second, thread synchronization and lock
To illustrate thread synchronization and locking, the best example might be a ticketing system that sells multiple windows at the same time. We know that in Java, synchronized is used to synchronize, while the iphone does not provide a Java-like synchronized keyword, but it provides a Nscondition object interface. Viewing Nscondition's interface description shows 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 Internet:
SellTicketsAppDelegate.h file
sellticketsappdelegate.himport <UIKit/UIKit.h> @interface Sellticketsappdelegate:nsobject < uiapplicationdelegate> { int tickets; int count; nsthread* Ticketsthreadone; nsthread* Ticketsthreadtwo; nscondition* ticketscondition; UIWindow *window; } @property (nonatomic, retain) iboutlet UIWindow *window; @end
SELLTICKETSAPPDELEGATE.M file
Sellticketsappdelegate.mimport "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 to Customization after application launch [window makekeyandvisible]; } -(void) run{while (TRUE) {//Lock [ticketscondition lock]; if (Tickets > 0) {[Nsthread sleepfortimeinterval:0.5]; Count = 100-tickets; NSLog (@ "Current number of votes:%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 during the run, 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 in this procedure, you may need to release some resources, 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 anything, call autorelease inside the thread function, and the following error will appear:
NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
Four, about the thread pool, you can view the relevant information nsoperation.
Original posts: http://www.voland.com.cn/iphone-in-the-multi-threaded-programming
Multi-Threading for iOS development