IPhone MultithreadingIs the content to be introduced in this article,MultithreadingIt is difficult to implement in many programming languages. Although objective-c originated from cMultithreadingProgramming is quite simple and comparable to java. This article mainly coversThreadCreate and start,ThreadSynchronization and lock,ThreadInteraction,ThreadPool and so on.IphoneInMultithreadingProgramming.
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;
Ii. 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 implement it using NSCondition.IphoneInThreadSecurity. 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
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];
- }
Summary: AnalysisIPhone MultithreadingI hope this article will help you!