欢迎访问移动开发之家(rcyd.net),关注移动开发教程。移动开发之家  移动开发问答|  每日更新
页面位置 : > > > 内容正文

IOS开发:多线程NSThread和NSInvocationOperation

来源: 开发者 投稿于  被查看 8158 次 评论:282

IOS开发:多线程NSThread和NSInvocationOperation


   多线程编程是防止主线程堵塞,增加运行效率等等的最佳方法。而原始的多线程方法存在很多的毛病,包括线程锁死等。在Cocoa中,Apple提供了NSOperation这个类,提供了一个优秀的多线程编程方法。

  本次介绍NSOperation的子集,简易方法的NSInvocationOperation:

  @implementation MyCustomClass

  -(void)launchTaskWithData:(id)data

  {

  //创建一个NSInvocationOperation对象,并初始化到方法;

  //在这里,selector参数后的值是你想在另外一个线程中运行的方法(函数,Method);

  //在这里,object后的值是想传递给前面方法的数据

  NSInvocationOperation *theOp = [[NSInvocationOperation alloc]initWithTarget:self selector:@selector(myTashMethod:) object:data];

  //下面将我们建立的操作"Operation"加入到本地程序的共享的队列中(加入后方法就会立刻被执行)

  //更多的时候是由我们自己建立“操作”队列

  [[MyAppDelegate sharedOperationQueue] addOperation:theOp];

  }

  //这个是真正运行在另外一个线程的“方法”

  - (void)myTaskMethod:(id)data

  {

  // Perform the task.

  }

  @end

  //一个NSOperationQueue 操作队列,就相当于一个线程管理器,而非一个线程。因为你可以设置这个线程管理器内可以并行运行的的线程数量等等。

  //下面是建立并初始化一个操作队列:

  @interface MyViewController : UIViewController {

  NSOperationQueue* operationQueue;

  //在头文件中声明该队列

  }

  @end

  @implementation MyViewController

  -(id)init

  {

  self = [super init];

  if (self) {

  //初始化操作队列

  operationQueue = [[NSOperationQueue alloc] init];

  [operationQueue setMaxConcurrentOperationCount:1];

  //在这里限定了该队列只同时运行一个线程

  //这个队列已经可以使用了

  }

  return self;

  }

  - (void)dealloc

  {

  [operationQueue release];

  [super dealloc];

  }

  @end

  //简单介绍之后,其实可以发现这种方法是非常简单的。很多的时候我们使用多线程仅仅是为了防止主线程堵塞,而NSInvocationOperation就是最简单的多线程编程,在iPhone编程中是经常被用到的。

  /////////////////////////////////////////////////////////////

  //在主线程里加入一个loading画面……

  {

  [window addSubview:view_loading];

  //另一个新的线程,可能需要时间进行后台处理,为了防止主程序在这段时间静止等待,将后台处理放在主线程之外的线程执行,执行完以后,通知主线程更新数据。

  [NSThread detachNewThreadSelector:@selector:(init_backup:) toTarget:self withObject:nil];

  }

  //可以通过performSelectorOhMainThread更新UI元素,比如设置进度条等等。最后消除loading画面,载入主View。

  -(void)init_backup:(id)sender

  {

  NSAutorelease* pool = [[NSAutoreleasePool alloc] init];

  //新建的线程需要一个自动释放池对线程中申请的内存进行管理

  int i = status;

  [self performSelectorOnMainThread:@selector:(show_loading:) wiwithObject::[NSNumber numberWithInt:i] waitUntil Done:NO];

  [view_loading removeFromSuperview];

  [window addSubview:tabcontroller_main.view];

  [pool release];

  }

  利用iphone的多线程实现和线程同步

  从接口的定义中可以知道,NSThread和大多数iphone的接口对象一样,有两种方式可以初始化:

  一种使用initWithTarget :(id)target selector:(SEL)selector object:(id)argument,但需要负责在对象的retain count为0时调用对象的release方法清理对象。

  另一种则使用所谓的convenient method,这个方便接口就是detachNewThreadSelector,这个方法可以直接生成一个线程并启动它,而且无需为线程的清理负责。

  #import

  @interface SellTicketsAppDelegate : NSObject

  {

  int tickets;

  int count;

  NSThread* ticketsThreadone;

  NSThread* ticketsThreadtwo;

  NSCondition* ticketsCondition;

  UIWindow *window;

  }

  @property (nonatomic, retain) IBOutlet UIWindow *window;

  @end

  //SellTicketsAppDelegate.m

  #import "SellTicketsAppDelegate.h"

  @implementation SellTicketsAppDelegate

  @synthesize window;

  - (void)applicationDidFinishLaunching:(UIApplication *)application

  {

  tickets = 100;

  count = 0;

  // 锁对象

  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) {

  //上锁

  [ticketsCondition lock];

  if(tickets > 0)

  {

  [NSThread sleepForTimeInterval:0.5];

  count = 100 - tickets;

  NSLog(@"当前票数是:%d,售出:%d,线程名:%@",tickets,count,[[NSThread currentThread] name]);

  tickets--;

  }

  else

  {

  break;

  }

  [ticketsCondition unlock];

  }

  -(void)dealloc{

  [ticketsThreadone release];

  [ticketsThreadtwo release];

  [ticketsCondition release];

  [window release];

  [super dealloc];

  }

相关文章

    暂无相关文章

用户评论