Analysis of iPhone Multithreading

Source: Internet
Author: User

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:

 
 
  1. - (id)init; // designated initializer  
  2. - (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:

 
 
  1. + (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:

 
 
  1. - (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:

 
 
  1. SellTicketsAppDelegate. h file
  2.  
  3. // SellTicketsAppDelegate. h
  4. Import <UIKit/UIKit. h>
  5.  
  6. @ Interface SellTicketsAppDelegate: NSObject <UIApplicationDelegate> {
  7. Int tickets;
  8. Int count;
  9. NSThread * ticketsThreadone;
  10. NSThread * ticketsThreadtwo;
  11. NSCondition * ticketsCondition;
  12. UIWindow * window;
  13. }
  14. @ Property (nonatomic, retain) IBOutlet UIWindow * window;
  15. @ End
  16.  
  17. SellTicketsAppDelegate. m file
  18.  
  19. // SellTicketsAppDelegate. m
  20. Import "SellTicketsAppDelegate. h"
  21. @ Implementation SellTicketsAppDelegate
  22. @ Synthesize window;
  23. -(Void) applicationDidFinishLaunching :( UIApplication *) application {
  24. Tickets = 100;
  25. Count = 0;
  26. // Lock Object
  27. TicketCondition = [[NSCondition alloc] init];
  28. TicketsThreadone = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
  29. [TicketsThreadone setName: @ "Thread-1"];
  30. [TicketsThreadone start];
  31. TicketsThreadtwo = [[NSThread alloc] initWithTarget: self selector: @ selector (run) object: nil];
  32. [TicketsThreadtwo setName: @ "Thread-2"];
  33. [TicketsThreadtwo start];
  34. // [NSThread detachNewThreadSelector: @ selector (run) toTarget: self withObject: nil];
  35. // Override point for customization after application launch
  36. [Window makeKeyAndVisible];
  37.  
  38. }
  39. -(Void) run {
  40. While (TRUE ){
  41. // Lock
  42. [TicketsCondition lock];
  43. If (tickets> 0 ){
  44. [NSThread sleepForTimeInterval: 0.5];
  45. Count = 100-tickets;
  46. NSLog (@ "Current ticket count: % d, sold: % d, thread name: % @", tickets, count, [[NSThread currentThread] name]);
  47. Tickets --;
  48. } Else {
  49. Break;
  50. }
  51. [TicketsCondition unlock];
  52. }
  53. }
  54. -(Void) dealloc {
  55. [TicketsThreadone release];
  56. [TicketsThreadtwo release];
  57. [TicketsCondition release];
  58. [Window release];
  59. [Super dealloc];
  60. }
  61. @ 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:

 
 
  1. - (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:

 
 
  1. - (void)startTheBackgroundJob {  
  2.     NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];  
  3.     // to do something in your thread job  
  4.     ...  
  5.     [self performSelectorOnMainThread:@selector(makeMyProgressBarMoving) withObject:nil waitUntilDone:NO];  
  6.     [pool release];  

Summary: AnalysisIPhone MultithreadingI hope this article will help you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.