iOS学习笔记03—多线程

本文介绍了iOS中的多线程概念及实现方式,包括线程、进程和任务的区别,以及使用NSThread、NSOperation和GCD的具体方法。通过实例展示了如何在iOS应用中实现图片下载与移动等功能。

一.线程术语

  1. 线程(线程)用于指代独立执行的代码段。
  2. 进程(process)用于指代一个正在运行的可执行程序,它可以包含多个线程。
  3. 任务(task)用于指代抽象的概念,表示需要执行工作。
二.多线程实现方法

主要方法:1、NSThread。2、NSOperation。3、GCD。

 

1、  NSThread:

调用方法如下:如果需要函数参数的话,可以通过Object传递。

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

 

1.2:NSThread* myThread = [[NSThread alloc] initWithTarget:self selector:@selector(threadInMainMethod:) object:nil];

 [myThread start]; 

1.3:  [obj performSelectorInBackground:@selector(threadMe) withObject:nil];

 

2、  NSOperation:

NSOperation也是多线程的一种,NSOpertaion有2种形式.

(1) 并发执行

       并发执行你需要重载如下4个方法

     //执行任务主函数,线程运行的入口函数

    - (void)start 

       //是否允许并发,返回YES,允许并发,返回NO不允许。默认返回NO

    -(BOOL)isConcurrent 

    - (BOOL)isExecuting

     //是否已经完成,这个必须要重载,不然放在放在NSOperationQueue里的NSOpertaion不能正常释放。

   - (BOOL)isFinished

   

   比如TestNSOperation:NSOperaion 重载上述的4个方法,

   声明一个NSOperationQueue, NSOperationQueue *queue = [[[NSOperationQueue alloc ] init] autorelease];

  [queue addOperation:testNSoperation];

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

 

(2)非并发执行

  -(void)main

   只需要重载这个main方法就可以了。 

 

3、  GCD

GCD很强大,我的使用经验很少。但是scorpiozj 总结的比较全面(http://www.cnblogs.com/scorpiozj/archive/2011/07/25/2116459.html

同时,这篇文章也介绍的比较详细 http://www.cnblogs.com/vinceoniphone/archive/2011/04/07/2007968.html 

官方教程

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

下面首先来看GCD的使用:

1

dispatch_async(dispatch_queue_t queue, dispatch_block_t block);

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

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

1. Main queue:

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

2.Serial quque(private dispatch queue)

  每次运行一个任务,可以添加多个,执行次序FIFO. 通常是指程序员生成的,比如:

1

2

3

4

NSDate *da = [NSDate date];

NSString *daStr = [da description];

const char *queueName = [daStr UTF8String];

dispatch_queue_t myQueue = dispatch_queue_create(queueName, NULL);

3. Concurrent queue(global dispatch queue):

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

所以我们可以大致了解使用GCD的框架:

1

2

3

4

5

6

7

dispatch_async(getDataQueue,^{

    //获取数据,获得一组后,刷新UI.

    dispatch_aysnc (mainQueue, ^{

    //UI的更新需在主线程中进行

};

}

)

由此可见,GCD的使用非常简单,以我的使用经验来看,以后会逐步淘汰使用NSOperation而改用GCD。

三.iOS多线程编程

效果图


定义宏

1

2

3

#define CENTER1 CGPointMake(100,90)

#define CENTER2 CGPointMake(100,370)

#define URL_STRING @"http://i03.c.aliimg.com/news/upload/5000180/news/2007/9/30/400x400_11555946b0c.jpg"

3个Button实现不同功能 第一个是单一进程 实现下载图片 


-(void)normal:(UIButton *)s

{

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

    NSURL *url =[NSURL URLWithString:URL_STRING];

    NSData *imageData =[NSData dataWithContentsOfURL:url];

    UIImage *images =[UIImage imageWithData:imageData];

    [image setImage:images];

    [pool drain];

    [pool release];   

}



第二个Button实现的是图片的移动


-(void)move:(UIButton *)s

{

    [UIView beginAnimations:nil context:NULL];

    image.center =CGPointEqualToPoint(image.center, CENTER2)?CENTER1:CENTER2;

    [UIView commitAnimations]; 

}


第三个Button实现的是图片的下载和移动


-(void)thread:(UIButton *)s

{

    NSInvocationOperation *operation =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(normal:) object:nil];

    NSInvocationOperation *operation1 =[[NSInvocationOperation alloc]initWithTarget:self selector:@selector(move:) object:nil];

    NSOperationQueue *queue =[[NSOperationQueue alloc]init];

    [queue setMaxConcurrentOperationCount:1];//设置并发线程数

    [queue addOperation:operation];

    [queue addOperation:operation1];

   [operation release];

    [queue release];

   [operation1 release];

}



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值