IOS MultithreadingThe first experience is what we will introduce in this article.ThreadThe application is not uncontrolled. Official documents show thatThreadThe stack size of is 1 M, and the secondThreadAt the beginning, the data volume is kb. And the value cannot be switched through the compiler orThreadAPI function. Only MasterThreadAbility to directly modify the UI.
I. NSOperation and NSOperationQueue
1. An operation class inherited from NSOperation. The implementation of this class must contain the-(void) main method.
2. The simplest way to use NSOperation is to put it into NSOperationQueue.
Once an operation is added to the queue, the queue starts and starts to process it, that is, the main method of the operation class is called ). Once this operation is completed, the queue will release it.
- 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 maximum number of operations in the Operation queue: [queue setMaxConcurrentOperationCount: 2];
Ii. NSThread
1. Thread creation and startup
There are two main ways to create a thread:
- - (id)init; // designated initializer
- - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument;
Of course, there is another special kind, that is, the so-called convenient method, which can directly generate a thread and start it, without being responsible for thread cleaning. The interface for this method is:
- + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument
After the first two methods are created, the mobile phone needs to be started. The start method is as follows:
- - (void)start;
2. Thread Synchronization and lock
To describe the synchronization and lock of threads, the best example may be the ticket sales system with multiple windows at the same time. We know that synchronized is used for synchronization in java. Although the iphone does not provide a keyword similar to synchronized in java, it provides an NSCondition object interface. View the NSCondition interface description. We can see that NSCondition is the lock object under the iphone, so we can use NSCondition to implement thread security in the iphone. This is an example from the Internet:
SellTicketsAppDelegate. h file
- // SellTicketsAppDelegate. h
- Import <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;
- @ EndSellTicketsAppDelegate. 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 ){
- // Lock
- [TicketsCondition lock];
- If (tickets> 0 ){
- [NSThread sleepForTimeInterval: 0.5];
- Count = 100-tickets;
- NSLog (@ "Current ticket count: % 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
3. Thread Interaction
When a thread is running, it may need to communicate with other threads, such as modifying the interface in the main thread. You can use the following interface:
- - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
Because some resources may need to be released during this process, you need to use the NSAID utoreleasepool for management, such:
- - (void)startTheBackgroundJob {
- NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
- // to do something in your thread job
- ...
- [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];
- [pool release];
- }
If you do not consider anything and call autorelease in the thread function, the following error will occur:
- NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
4. For more information about the thread pool, see NSOperation.
Summary:IOS MultithreadingI hope this article will help you with the introduction of the first experience.