打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
剖析iPhone多线程

iPhone多线程是本文要介绍的内容,多线程在各种编程语言中都是难点,很多语言中实现起来很麻烦,objective-c虽然源于c,但其多线程编程却相当简单,可以与java相媲美。这篇文章主要从线程创建与启动、线程的同步与锁、线程的交互、线程池等等四个方面简单的讲解一下iphone中的多线程编程。

一、线程创建与启动

线程创建主要有二种方式:

  1. - (id)init; // designated initializer  
  2. - (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; 

当然,还有一种比较特殊,就是使用所谓的convenient method,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。这个方法的接口是:

  1. + (void)detachNewThreadSelector:(SEL)aSelector toTarget:(id)aTarget withObject:(id)anArgument 

前两种方法创建后,需要手机启动,启动的方法是:

  1. - (void)start; 

二、线程的同步与锁

要说明线程的同步与锁,最好的例子可能就是多个窗口同时售票的售票系统了。我们知道在java中,使用synchronized来同步,而iphone虽然没有提供类似java下的synchronized关键字,但提供了NSCondition对象接口。查看NSCondition的接口说明可以看出,NSCondition是iphone下的锁对象,所以我们可以使用NSCondition实现iphone中的线程安全。这是来源于网上的一个例子:

  1. SellTicketsAppDelegate.h 文件  
  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 文件  
  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.      // 锁对象  
  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.      // 上锁  
  42.          [ticketsCondition lock];  
  43.          if(tickets > 0){  
  44.              [NSThread sleepForTimeInterval:0.5];  
  45.              count = 100 - tickets;  
  46.              NSLog(@"当前票数是:%d,售出:%d,线程名:%@",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 

三、线程的交互

线程在运行过程中,可能需要与其它线程进行通信,如在主线程中修改界面等等,可以使用如下接口:

  1. - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait 

由于在本过程中,可能需要释放一些资源,则需要使用NSAutoreleasePool来进行管理,如:

  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];  

小结:剖析iPhone多线程的内容介绍完了,希望本文对你有所帮助!

【编辑推荐】

【责任编辑:李程站 TEL:(010)68476606】

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
ios 多线程
iOS之多线程编程:三个层次线程应用
iOS开发多线程之总结篇(常见用法、常用方法)
NSThread 停止线程
多线程操作数据库 (CoreData)
[Cocoa]深入浅出 Cocoa 之多线程 NSThread
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服