解析在iPhone应用中NSThread创建Run Loop

移动开发 iOS
IPhone多线程编程提议用NSOperation和NSOperationQueue,这个确实很好用。但是有些情况下,我们还是在运行一些长线任务或者复杂任务的时候需要用比较原始的NSThread。来看内容。

iPhone应用中NSThread创建Run Loop是本文要介绍的内容,虽然iphone为我们提供了很多简单易于操作的线程方法。IPhone多线程编程提议用NSOperation和NSOperationQueue,这个确实很好用。但是有些情况下,我们还是在运行一些长线任务或者复杂任务的时候需要用比较原始的NSThread。这就需要为NSThread创建一个run loop.

  1. NSThread *thread1 = [[NSThread alloc] initWithTarget:self selector:@selector(playerThread: ) object:nil];  
  2. [thread start];  
  3. //如果要利用NSOperation,原理类似。只需要加入到queue里面去就好了。。queue会在合适的时机调用方法,下面代码作为参考。  
  4. - (void) playerThread: (void*)unused   
  5. {   
  6. audioRunLoop = CFRunLoopGetCurrent();//子线程的runloop引用  
  7. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];//子线程的  
  8. run loop [self initPlayer]; CFRunLoopRun(); //运行子线程的  
  9. run loop,这里就会停住了。 [pool release];  
  10. }  
  11.  // 实现一个timer,用于检查子线程的工作状态,并在合适的时候做任务切换。或者是合适的时候停掉自己的  
  12.  run loop-(void) initPlayer {   
  13.  // 在这里你可以初始化一个工作类,比如声音或者视频播放   
  14.  NSTimer *stateChange = [NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:  
  15.    @selector(checkStatuserInfo:nil repeats:YES];  
  16.  }  
  17.  -(void) checkState:(NSTimer*) timer   
  18.  {   
  19.  if(需要退出自线程了) {  
  20.  //释放子线程里面的资源  
  21.  CFRunLoopStop( CFRunLoopGetCurrent());//结束子线程任务  
  22.  }   

小结:解析在iPhone应用中NSThread创建run loop的内容介绍完了,希望本文对你有所帮助!

【编辑推荐】

  1. 关于iPhone多线程编程的教程
  2. iPhone游戏开发者需知6点建议
  3. iPhone开发应用之Archiving NSCoder教程
  4. iPhone开发应用中关于CFRunLoop学习
  5. 关于iPhone开发类NSDate常用代码案例

 

责任编辑:zhaolei 来源: 互联网
相关推荐

2013-08-21 16:17:09

iPhone多线程

2010-11-05 13:02:58

内存iPhone

2013-07-16 11:38:46

iOS多线程多线程概念GCD

2011-07-27 14:56:07

iPhone Makefile Dylib

2022-04-18 08:34:29

回调函数命令解析

2011-08-12 14:54:45

iPhone委托

2011-08-19 13:45:14

iPhone应用iPhone OS数据

2011-08-02 17:27:06

iPhone应用 剪切技巧

2011-08-02 17:14:41

iPhone应用 UITableVie

2011-08-10 17:37:00

iPhoneASIHTTPRequ

2010-05-06 16:30:49

Oracle for

2011-07-26 14:18:20

2011-09-15 15:58:37

iPhone应用Quick Snap拍摄工具

2011-08-22 14:12:48

iPhone开发NSTableView

2011-08-15 11:37:20

iPhone开发Mask

2011-08-15 15:44:46

iPhone开发PDF

2011-08-09 17:12:30

iPhoneCFRunLoop

2011-08-12 14:33:06

iPhone缓存文件

2011-08-18 16:24:44

iPhone开发图片

2011-08-08 15:48:13

iPhone TableView 背景
点赞
收藏

51CTO技术栈公众号