转载的一篇关于iOS里多线程NSThread/NSOperation/GCD的文章

转载

IOS多线程编程对于初学者来说,总是会觉得很难理解和掌握,现在通过几个实例来更加系统全面的理解IOS多线程编程,希望对大家有所帮助。

1:首先简单介绍什么叫线程
可并发执行的,拥有最小系统资源,共享进程资源的基本调度单位。
共用堆,自有栈(官方资料说明iOS主线程栈大小为1M,其它线程为512K)。
并发执行进度不可控,对非原子操作易造成状态不一致,加锁控制又有死锁的风险。

2:IOS中的线程
iOS主线程(UI线程),我们的大部分业务逻辑代码运行于主线程中。
没有特殊需求,不应引入线程增加程序复杂度。
应用场景:逻辑执行时间过长,严重影响交互体验(界面卡死)等。

IOS 多线程 有三种主要方法
(1)NSThread
(2)NSOperation
(3)**

1. Main queue:
顾名思义,运行在主线程,由dispatch_get_main_queue获得.和ui相关的就要使用Main Queue.

2.Serial quque(private dispatch queue)
每次运行一个任务,可以添加多个,执行次序FIFO(队列,先进先出first input first out). 通常是指程序员生成的,比如:
  1. dispatch_queue_t myCustomQueue = dispatch_queue_create("example.MyCustomQueue", NULL);dispatch_async(myCustomQueue, ^{
  2.     for (int abc=0;abc<100;abc++)
  3.         {
  4.             printf("1.Do some work here.\n"); 
  5.        }
  6.     });dispatch_async(myCustomQueue, ^{
  7.     for (int abc=0;abc<100;abc++) 
  8.        {
  9.             printf("2.Do some work here.\n");  
  10.           } 
  11.    });dispatch_queue_t myCustomQueue2 = dispatch_queue_create("example.MyCustomQueue2", NULL);dispatch_async(myCustomQueue2, ^{
  12.     for (int abc=0;abc<100;abc++)
  13.         {
  14.             printf("1. myCustomQueue2 Do some work here.\n");
  15.         } 
  16.    });dispatch_async(myCustomQueue2, ^{
  17.     for (int abc=0;abc<100;abc++)  
  18.       {  
  19.           printf("2. myCustomQueue2 Do some work here.\n");  
  20.       }    
  21. });
复制代码

然而,因为myCustomQueue 和 myCustomQueue2 是在两个队列中,所以在队列myCustomQueue中:“1.Do some work here.” 在 “2.Do some work here.” 之前,而在myCustomQueue2队列中:“1. myCustomQueue2 Do some work here.”在“2. myCustomQueue2 Do some work here.”之前。而在myCustomQueue和myCustomQueue2之中的任务是没有先后的。及不是并发的。

3. Concurrent queue(global dispatch queue):
可以同时运行多个任务,每个任务的启动时间是按照加入queue的顺序,结束的顺序依赖各自的任务.使用dispatch_get_global_queue获得.

下面简单介绍这三个方法
1.NSThread

调用方法如下:
如函数需要输入参数,可从object传进去。
  1.     (1) [NSThread detachNewThreadSelector:@selector(threadInMainMethod:) toTarget:self withObject:nil];
  2.     (2) NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];
  3. [myThread start];
  4.     (3) [obj performSelectorInBackground:@selector(threadMe) withObject:nil];
复制代码

提个问题:如果某个ViewController里运行了一个Thread,Thread还没结束的时候,这个ViewController被Release了,结果会如何?

经过的的测试,Thread不结束,ViewController一直保留,不会执行dealloc方法。

2.NSOperation 
NSoperation也是多线程的一种,NSopertaion有2种形式
  
(1) 并发执行
并发执行你需要重载如下4个方法     
  1. //执行任务主函数,线程运行的入口函数
  2.     - (void)start
  3.        //是否允许并发,返回YES,允许并发,返回NO不允许。默认返回NO
  4.     -(BOOL)isConcurrent
  5.     - (BOOL)isExecuting
  6.      //是否已经完成,这个必须要重载,不然放在放在NSOperationQueue里的NSOpertaion不能正常释放。
  7.    - (BOOL)isFinished
复制代码

比如TestNSOperation:NSoperaion 重载上述的4个方法,
   
声明一个NSOperationQueue, 
  1. NSOperationQueue *queue = [[[NSOperationQueue alloc ] init] autorelease];
  2.   [queue addOperation:testNSoperation];
复制代码

它会自动调用TestNSOperation里的 start函数,如果需要多个NSOperation,你需要设置queue的一些属性,如果多个NSOperation之间又依赖关系,也可以设置,具体可以参考API 文档。

(2)非并发执行
-(void)main
只需要重载这个main方法就可以了。

3.**

**很强大,我的使用经验很少。但是scorpiozj 总结的比较全面(http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html
  
同时,这篇文章也介绍的比较详细 http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html官方教程

**是和block紧密相连的,所以最好先了解下block(可以查看这里).**是C level的函数,这意味着它也提供了C的函数指针作为参数,方便了C程序员.

下面首先来看**的使用:
  1. dispatch_async(dispatch_queue_t queue, dispatch_block_t block);
复制代码

async表明异步运行,block代表的是你要做的事情,queue则是你把任务交给谁来处理了.(除了async,还有sync,delay,本文以async为例).

之所以程序中会用到多线程是因为程序往往会需要读取数据,然后更新UI.为了良好的用户体验,读取数据的操作会倾向于在后台运行,这样以避免阻塞主线程.**里就有三种queue来处理.

所以我们可以大致了解使用**的框架:
  1. dispatch_queue_t newThread = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_async(newThread,^{[self
  2. downloadImage:ImageURL];  });
复制代码

小节:NSThread的方式或许能做更快的切换,因为ARMv6或更高版本的处理器都提供了非常强大的线程切换机制。但是NSThread不会采取多核的分派,因为这个系统接口首先要保证的是用户线程的可靠性。 而Grand Central Dispatch显式地利用分派队列来做多核分派调度,因此如果是在多核处理器上的话用G_C_D更快。如果你的处理器是单核心的话,那么可以使用切换更快NSThread。
    
你每天早晨醒来为之奋斗的目标,它应是你人生的目标。

原文链接:http://www.gsdtarena.com/a/iosjc/616_2.html
posted @ 2015-06-29 19:20  有码才合法  阅读(175)  评论(0编辑  收藏  举报