IPhone MultithreadingThe Programming Tutorial is the content to be introduced in this article.MultithreadingSo this article can also be used as a reference for learning. Let's talk about it in detail.MultithreadingProgramming.
Multi-thread programming in iphone: thread Creation
MultithreadingIt is difficult to implement in many programming languages. Although objective-c originated from c, its multi-threaded programming is quite simple and comparable to java.
I. 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;
Multi-thread programming in iphone: synchronization and lock of threads
DescriptionThreadThe best example of synchronization and lock is the ticketing system that sells tickets at the same time in multiple windows. 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;
- @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 ){
- // 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
Multi-thread programming in iphone: thread Interaction
ThreadDuring runningThreadFor communication, suchThreadYou can use the following interfaces:
- - (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 anythingThreadIf autorelease is called in the function, the following error occurs:
- NSAutoReleaseNoPool(): Object 0x********* of class NSConreteData autoreleased with no pool in place ….
Summary: AboutIPhone MultithreadingThe content of the Programming Tutorial is complete. I hope this article will help you!