标签:
一、进程的概念
运行在内存中的程序。(CPU为其分配内存)
二、线程的概念
进程中执行的操作。每一个进程中,至少有一个线程(主线程),主线程的栈区大小为1M,自县城的栈区大小为512K。
三、开启多线程的方法:
1、NSThread:是三种开启多线程的方法中相对轻量级的,但需要管理生命周期、同步、枷锁等问题,这会导致一定的性能开销。
2、NSOperatios:是基于OC实现的,NSOperation以面向对象的方式封装了所需要执行的操作,不必关心线程管理、同步等问题。NSOPeration是一个抽象基类,iOS提供了两种默认实现NSIvocationOperation和NSBlockOperation,当然也可以自定义NSOperation。
3、GCD:提供了一些新特性、运行库来支持多核并行编程,他的关注点更高,如何在多个CPU上提升效率。
四、NSThread的初始化
1、动态方法:
// 初始化线程 NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil]; // 设置线程的优先级(0.0 - 1.0,1.0最高级) thread.threadPriority = 1; // 开启线程 [thread start]; //selector :线程执行的方法,这个selector最多只能接收一个参数 //target :selector消息发送的对象 //argument : 传给selector的唯一参数,也可以是nil
2、静态方法:
[NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil]; // 调用完毕后,会马上创建并开启新线程
3、隐式创建线程的方法:
[self performSelectorInBackground:@selector(run) withObject:nil];
五、获取当前线程
NSThread *current = [NSThread currentThread];
六、获取主线程
NSThread *main = [NSThread mainThread];
七、暂停当前线程
// 暂停2s [NSThread sleepForTimeInterval:2]; // 或者 NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]]; [NSThread sleepUntilDate:date];
八、线程间的通讯
1、在指定线程上执行操作
[self performSelector:@selector(run) onThread:thread withObject:nil waitUntilDone:YES];
2、在主线程上执行操作
[self performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
3、在当前线程上执行操作
[self performSelector:@selector(run) withObject:nil];
九、优缺点
1、优点:NSThread比其他两种多线程方案较轻量级,更直观地控制线程对象
2、缺点:需要自己管理线程的生命周期,线程同步。线程同步对数据的加锁会有一定的系统开销
十、示例
// // NSThread实现多线程 // MultiThread // // Created by Kenshin Cui on 14-3-22. // Copyright (c) 2014年 Kenshin Cui. All rights reserved. // #import "KCMainViewController.h" @interface KCMainViewController (){ UIImageView *_imageView; } @end @implementation KCMainViewController - (void)viewDidLoad { [super viewDidLoad]; [self layoutUI]; } #pragma mark 界面布局 -(void)layoutUI{ _imageView =[[UIImageView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame]; _imageView.contentMode=UIViewContentModeScaleAspectFit; [self.view addSubview:_imageView]; UIButton *button=[UIButton buttonWithType:UIButtonTypeRoundedRect]; button.frame=CGRectMake(50, 500, 220, 25); [button setTitle:@"加载图片" forState:UIControlStateNormal]; //添加方法 [button addTarget:self action:@selector(loadImageWithMultiThread) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:button]; } #pragma mark 将图片显示到界面 -(void)updateImage:(NSData *)imageData{ UIImage *image=[UIImage imageWithData:imageData]; _imageView.image=image; } #pragma mark 请求图片数据 -(NSData *)requestData{ NSURL *url=[NSURL URLWithString:@"http://images.apple.com/iphone-6/overview/images/biggest_right_large.png"]; NSData *data=[NSData dataWithContentsOfURL:url]; return data; } #pragma mark 加载图片 -(void)loadImage{ //请求数据 NSData *data= [self requestData]; /*将数据显示到UI控件,注意只能在主线程中更新UI, 另外performSelectorOnMainThread方法是NSObject的分类方法,每个NSObject对象都有此方法, 它调用的selector方法是当前调用控件的方法,例如使用UIImageView调用的时候selector就是UIImageView的方法 Object:代表调用方法的参数,不过只能传递一个参数(如果有多个参数请使用对象进行封装) waitUntilDone:是否线程任务完成执行 */ [self performSelectorOnMainThread:@selector(updateImage:) withObject:data waitUntilDone:YES]; } #pragma mark 多线程下载图片 -(void)loadImageWithMultiThread{ //方法1:使用对象方法 //创建一个线程,第一个参数是请求的操作,第二个参数是操作方法的参数 // NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(loadImage) object:nil]; // //启动一个线程,注意启动一个线程并非就一定立即执行,而是处于就绪状态,当系统调度时才真正执行 // [thread start]; //方法2:使用类方法 [NSThread detachNewThreadSelector:@selector(loadImage) toTarget:self withObject:nil]; } @end
标签:
原文地址:http://www.cnblogs.com/zhuyiios/p/5371766.html