iOS开发多线程篇—NSThread及线程的状态
2015-07-29 14:16
639 查看
优点:NSThread 比其他两个轻量级
缺点:需要自己管理线程的生命周期(线程状态);并通过[b]对数据的加锁来实现线程安全,会有一定的系统开销。[/b]
本章主要围绕NSThread的优缺点展开,分析如下几点:
1、如何创建、启动NSThread
2、如何管理线程的生命周期,即线程状态
3、如何解决线程安全问题?
一、NSThread创建和启动
一个NSThread对象就代表一条线程
创建、启动线程
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 线程一启动,就会在线程thread中执行self的run方法
主线程相关用法
+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程
其他用法
获得当前线程
NSThread *current = [NSThread currentThread];
线程的调度优先级:调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
设置线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他创建线程的方式
(2)创建线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式创建并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述2种创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置
[代码示例]
1.使用古老的方式创建
实现效果:
![](http://images.cnitblog.com/i/450136/201406/241242151428203.png)
打印结果:
![](http://images.cnitblog.com/i/450136/201406/241243124086331.png)
2.使用NSThread创建线程
调用线程1,打印结果为:
![](http://images.cnitblog.com/i/450136/201406/241244014249240.png)
调用线程2
![](http://images.cnitblog.com/i/450136/201406/241244511116819.png)
调用线程3
![](http://images.cnitblog.com/i/450136/201406/241245565809853.png)
二、线程状态
线程的创建:
self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
说明:创建线程有多种方式,这里不做过多的介绍。
![](http://images.cnitblog.com/i/450136/201406/242256335021175.png)
线程的开启:
[self.thread start];
![](http://images.cnitblog.com/i/450136/201406/242259355491524.png)
线程的运行和阻塞:
(1)设置线程阻塞1,阻塞2秒
[NSThread sleepForTimeInterval:2.0];
(2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
[NSThread sleepUntilDate:date];
![](http://images.cnitblog.com/i/450136/201406/242300591278157.png)
线程处理阻塞状态时在内存中的表现情况:(线程被移出可调度线程池,此时不可调度)
![](http://images.cnitblog.com/i/450136/201406/242303053149907.png)
线程的死亡:
当线程的任务结束,发生异常,或者是强制退出这三种情况会导致线程的死亡。
![](http://images.cnitblog.com/i/450136/201406/242304408926607.png)
线程死亡后,线程对象从内存中移除。
![](http://images.cnitblog.com/i/450136/201406/242306237525299.png)
[代码示例]
代码示例1:
打印查看:
![](http://images.cnitblog.com/i/450136/201406/242333415171545.png)
代码示例2(退出线程):
打印示例:
![](http://images.cnitblog.com/i/450136/201406/242336055496581.png)
注意:人死不能复生,线程死了也不能复生(重新开启),如果在线程死亡之后,再次点击屏幕尝试重新开启线程,则程序会挂。
![](http://images.cnitblog.com/i/450136/201406/242337493146714.png)
三、线程安全
多线程的安全隐患
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
比如多个线程访问同一个对象、同一个变量、同一个文件
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
示例一:
![](http://images.cnitblog.com/i/450136/201406/241255270965550.png)
示例二:
![](http://images.cnitblog.com/i/450136/201406/241256038464378.png)
问题代码:
打印结果:
![](http://images.cnitblog.com/i/450136/201406/241302364867997.png)
安全隐患分析
![](http://images.cnitblog.com/i/450136/201406/241303131584567.png)
![](http://images.cnitblog.com/i/450136/201406/241303355028628.png)
如何解决
互斥锁使用格式
@synchronized(锁对象) { // 需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
[代码示例]
执行效果图
![](http://images.cnitblog.com/i/450136/201406/241315433467454.png)
互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行任务
互斥锁,就是使用了线程同步技术
四:原子和非原子属性
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
atomic加锁原理
原子和非原子属性的选择
nonatomic和atomic对比
atomic:线程安全,需要消耗大量的资源
nonatomic:非线程安全,适合内存小的移动设备
iOS开发的建议
所有属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力
转自:http://www.cnblogs.com/wendingding/tag/多线程篇/
缺点:需要自己管理线程的生命周期(线程状态);并通过[b]对数据的加锁来实现线程安全,会有一定的系统开销。[/b]
本章主要围绕NSThread的优缺点展开,分析如下几点:
1、如何创建、启动NSThread
2、如何管理线程的生命周期,即线程状态
3、如何解决线程安全问题?
一、NSThread创建和启动
一个NSThread对象就代表一条线程
创建、启动线程
(1) NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil];
[thread start];
// 线程一启动,就会在线程thread中执行self的run方法
主线程相关用法
+ (NSThread *)mainThread; // 获得主线程
- (BOOL)isMainThread; // 是否为主线程
+ (BOOL)isMainThread; // 是否为主线程
其他用法
获得当前线程
NSThread *current = [NSThread currentThread];
线程的调度优先级:调度优先级的取值范围是0.0 ~ 1.0,默认0.5,值越大,优先级越高
+ (double)threadPriority;
+ (BOOL)setThreadPriority:(double)p;
设置线程的名字
- (void)setName:(NSString *)n;
- (NSString *)name;
其他创建线程的方式
(2)创建线程后自动启动线程 [NSThread detachNewThreadSelector:@selector(run) toTarget:self withObject:nil];
(3)隐式创建并启动线程 [self performSelectorInBackground:@selector(run) withObject:nil];
上述2种创建线程方式的优缺点
优点:简单快捷
缺点:无法对线程进行更详细的设置
[代码示例]
1.使用古老的方式创建
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 10 #import "YYViewController.h" 11 #import <pthread.h> 12 13 14 @interface YYViewController () 15 - (IBAction)btnClick; 16 @end 17 18 19 @implementation YYViewController 20 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 } 26 27 28 //按钮的点击事件 29 - (IBAction)btnClick { 30 //1.获取当前线程 31 NSThread *current=[NSThread currentThread]; 32 //主线程 33 NSLog(@"btnClick----%@",current); 34 35 //2.使用for循环执行一些耗时操作 36 pthread_t thread; 37 pthread_create(&thread, NULL, run, NULL); 38 } 39 40 41 //c语言函数 42 void *run(void *data) 43 { 44 //获取当前线程,是新创建出来的线程 45 NSThread *current=[NSThread currentThread]; 46 47 48 for (int i=0; i<10000; i++) { 49 NSLog(@"btnClick---%d---%@",i,current); 50 } 51 return NULL; 52 } 53 54 //多个线程,点击按钮执行按钮调用方法的时候,主线程没有被阻塞 55 56 @end 57 58
实现效果:
![](http://images.cnitblog.com/i/450136/201406/241242151428203.png)
打印结果:
![](http://images.cnitblog.com/i/450136/201406/241243124086331.png)
2.使用NSThread创建线程
1 // 2 // YYViewController.m 3 // 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 #import <pthread.h> 11 12 13 @interface YYViewController () 14 - (IBAction)btnClick; 15 @end 16 17 18 @implementation YYViewController 19 20 - (void)viewDidLoad 21 { 22 [super viewDidLoad]; 23 } 24 25 26 //按钮的点击事件 27 - (IBAction)btnClick { 28 //1.获取当前线程 29 NSThread *current=[NSThread currentThread]; 30 //主线程 31 NSLog(@"btnClick----%@",current); 32 33 //获取主线程的另外一种方式 34 NSThread *main=[NSThread mainThread]; 35 NSLog(@"主线程-------%@",main); 36 37 //2.执行一些耗时操作 38 [self creatNSThread]; 39 // [self creatNSThread2]; 40 // [self creatNSThread3]; 41 } 42 43 44 /** 45 * NSThread创建线程方式1 46 * 1> 先创建初始化线程 47 * 2> start开启线程 48 */ 49 -(void)creatNSThread 50 { 51 NSThread *thread=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程A"]; 52 //为线程设置一个名称 53 thread.name=@"线程A"; 54 //开启线程 55 [thread start]; 56 57 58 NSThread *thread2=[[NSThread alloc]initWithTarget:self selector:@selector(run:) object:@"线程B"]; 59 //为线程设置一个名称 60 thread2.name=@"线程B"; 61 //开启线程 62 [thread2 start]; 63 } 64 65 66 /** 67 * NSThread创建线程方式2 68 *创建完线程直接(自动)启动 69 */ 70 71 -(void)creatNSThread2 72 { 73 // NSThread *thread=[NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"]; 74 75 [NSThread detachNewThreadSelector:@selector(run:) toTarget:self withObject:@"创建完线程直接(自动)启动"]; 76 } 77 78 79 /** 80 * NSThread创建线程方式3 81 * 隐式创建线程, 并且直接(自动)启动 82 */ 83 84 -(void)creatNSThread3 85 { 86 //在后台线程中执行===在子线程中执行 87 [self performSelectorInBackground:@selector(run:) withObject:@"隐式创建"]; 88 } 89 90 91 92 -(void)run:(NSString *)str 93 { 94 //获取当前线程 95 NSThread *current=[NSThread currentThread]; 96 //打印输出 97 for (int i=0; i<10; i++) { 98 NSLog(@"run---%@---%@",current,str); 99 } 100 } 101 @end
调用线程1,打印结果为:
![](http://images.cnitblog.com/i/450136/201406/241244014249240.png)
调用线程2
![](http://images.cnitblog.com/i/450136/201406/241244511116819.png)
调用线程3
![](http://images.cnitblog.com/i/450136/201406/241245565809853.png)
二、线程状态
线程的创建:
self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil];
说明:创建线程有多种方式,这里不做过多的介绍。
![](http://images.cnitblog.com/i/450136/201406/242256335021175.png)
线程的开启:
[self.thread start];
![](http://images.cnitblog.com/i/450136/201406/242259355491524.png)
线程的运行和阻塞:
(1)设置线程阻塞1,阻塞2秒
[NSThread sleepForTimeInterval:2.0];
(2)第二种设置线程阻塞2,以当前时间为基准阻塞4秒
NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0];
[NSThread sleepUntilDate:date];
![](http://images.cnitblog.com/i/450136/201406/242300591278157.png)
线程处理阻塞状态时在内存中的表现情况:(线程被移出可调度线程池,此时不可调度)
![](http://images.cnitblog.com/i/450136/201406/242303053149907.png)
线程的死亡:
当线程的任务结束,发生异常,或者是强制退出这三种情况会导致线程的死亡。
![](http://images.cnitblog.com/i/450136/201406/242304408926607.png)
线程死亡后,线程对象从内存中移除。
![](http://images.cnitblog.com/i/450136/201406/242306237525299.png)
[代码示例]
代码示例1:
1 // 2 // YYViewController.m 3 // 04-NSThread02-线程的状态 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)NSThread *thread; 13 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //创建线程 23 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 24 //设置线程的名称 25 [self.thread setName:@"线程A"]; 26 } 27 //当手指按下的时候,开启线程 28 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 29 { 30 //开启线程 31 [self.thread start]; 32 } 33 34 -(void)test 35 { 36 //获取线程 37 NSThread *current=[NSThread currentThread]; 38 NSLog(@"test---打印线程---%@",self.thread.name); 39 NSLog(@"test---线程开始---%@",current.name); 40 41 //设置线程阻塞1,阻塞2秒 42 NSLog(@"接下来,线程阻塞2秒"); 43 [NSThread sleepForTimeInterval:2.0]; 44 45 //第二种设置线程阻塞2,以当前时间为基准阻塞4秒 46 NSLog(@"接下来,线程阻塞4秒"); 47 NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; 48 [NSThread sleepUntilDate:date]; 49 for (int i=0; i<20; i++) { 50 NSLog(@"线程--%d--%@",i,current.name); 51 52 } 53 NSLog(@"test---线程结束---%@",current.name); 54 } 55 56 @end
打印查看:
![](http://images.cnitblog.com/i/450136/201406/242333415171545.png)
代码示例2(退出线程):
1 // 2 // YYViewController.m 3 // 04-NSThread02-线程的状态 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 @property(nonatomic,strong)NSThread *thread; 13 14 @end 15 16 @implementation YYViewController 17 18 - (void)viewDidLoad 19 { 20 [super viewDidLoad]; 21 22 //创建线程 23 self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 24 //设置线程的名称 25 [self.thread setName:@"线程A"]; 26 } 27 //当手指按下的时候,开启线程 28 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 29 { 30 //开启线程 31 [self.thread start]; 32 } 33 34 -(void)test 35 { 36 //获取线程 37 NSThread *current=[NSThread currentThread]; 38 NSLog(@"test---打印线程---%@",self.thread.name); 39 NSLog(@"test---线程开始---%@",current.name); 40 41 //设置线程阻塞1,阻塞2秒 42 NSLog(@"接下来,线程阻塞2秒"); 43 [NSThread sleepForTimeInterval:2.0]; 44 45 //第二种设置线程阻塞2,以当前时间为基准阻塞4秒 46 NSLog(@"接下来,线程阻塞4秒"); 47 NSDate *date=[NSDate dateWithTimeIntervalSinceNow:4.0]; 48 [NSThread sleepUntilDate:date]; 49 for (int i=0; i<20; i++) { 50 NSLog(@"线程--%d--%@",i,current.name); 51 if (5==i) { 52 //结束线程 53 [NSThread exit]; 54 } 55 56 } 57 NSLog(@"test---线程结束---%@",current.name); 58 } 59 60 @end
打印示例:
![](http://images.cnitblog.com/i/450136/201406/242336055496581.png)
注意:人死不能复生,线程死了也不能复生(重新开启),如果在线程死亡之后,再次点击屏幕尝试重新开启线程,则程序会挂。
![](http://images.cnitblog.com/i/450136/201406/242337493146714.png)
三、线程安全
多线程的安全隐患
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
比如多个线程访问同一个对象、同一个变量、同一个文件
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
示例一:
![](http://images.cnitblog.com/i/450136/201406/241255270965550.png)
示例二:
![](http://images.cnitblog.com/i/450136/201406/241256038464378.png)
问题代码:
1 // 2 // YYViewController.m 3 // 05-线程安全 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 10 #import "YYViewController.h" 11 12 @interface YYViewController () 13 //剩余票数 14 15 @property(nonatomic,assign) int leftTicketsCount; 16 @property(nonatomic,strong)NSThread *thread1; 17 @property(nonatomic,strong)NSThread *thread2; 18 @property(nonatomic,strong)NSThread *thread3; 19 20 21 @end 22 23 24 @implementation YYViewController 25 26 27 - (void)viewDidLoad 28 { 29 [super viewDidLoad]; 30 31 //默认有20张票 32 33 self.leftTicketsCount=10; 34 35 //开启多个线程,模拟售票员售票 36 37 self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 38 39 self.thread1.name=@"售票员A"; 40 41 self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 42 43 self.thread2.name=@"售票员B"; 44 45 self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 46 self.thread3.name=@"售票员C"; 47 } 48 49 50 -(void)sellTickets 51 { 52 while (1) { 53 //1.先检查票数 54 int count=self.leftTicketsCount; 55 if (count>0) { 56 //暂停一段时间 57 [NSThread sleepForTimeInterval:0.002]; 58 59 //2.票数-1 60 self.leftTicketsCount= count-1; 61 62 //获取当前线程 63 NSThread *current=[NSThread currentThread]; 64 NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount); 65 }else 66 { 67 //退出线程 68 [NSThread exit]; 69 } 70 } 71 } 72 73 74 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 75 { 76 //开启线程 77 78 [self.thread1 start]; 79 [self.thread2 start]; 80 [self.thread3 start]; 81 82 } 83 84 @end
打印结果:
![](http://images.cnitblog.com/i/450136/201406/241302364867997.png)
安全隐患分析
![](http://images.cnitblog.com/i/450136/201406/241303131584567.png)
![](http://images.cnitblog.com/i/450136/201406/241303355028628.png)
如何解决
互斥锁使用格式
@synchronized(锁对象) { // 需要锁定的代码 }
注意:锁定1份代码只用1把锁,用多把锁是无效的
[代码示例]
1 // 2 // YYViewController.m 3 // 05-线程安全 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase. All rights reserved. 7 // 8 9 #import "YYViewController.h" 10 11 @interface YYViewController () 12 13 //剩余票数 14 @property(nonatomic,assign) int leftTicketsCount; 15 @property(nonatomic,strong)NSThread *thread1; 16 @property(nonatomic,strong)NSThread *thread2; 17 @property(nonatomic,strong)NSThread *thread3; 18 @end 19 20 @implementation YYViewController 21 22 - (void)viewDidLoad 23 { 24 [super viewDidLoad]; 25 //默认有20张票 26 self.leftTicketsCount=10; 27 //开启多个线程,模拟售票员售票 28 29 self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 30 31 self.thread1.name=@"售票员A"; 32 33 self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 34 35 self.thread2.name=@"售票员B"; 36 37 self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil]; 38 39 self.thread3.name=@"售票员C"; 40 } 41 42 43 -(void)sellTickets 44 { 45 while (1) { 46 @synchronized(self){//只能加一把锁 47 //1.先检查票数 48 49 int count=self.leftTicketsCount; 50 if (count>0) { 51 //暂停一段时间 52 [NSThread sleepForTimeInterval:0.002]; 53 //2.票数-1 54 55 self.leftTicketsCount= count-1; 56 //获取当前线程 57 NSThread *current=[NSThread currentThread]; 58 NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount); 59 60 }else 61 { 62 //退出线程 63 [NSThread exit]; 64 } 65 } 66 } 67 } 68 69 70 -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 71 { 72 73 //开启线程 74 [self.thread1 start]; 75 [self.thread2 start]; 76 [self.thread3 start]; 77 } 78 79 @end
执行效果图
![](http://images.cnitblog.com/i/450136/201406/241315433467454.png)
互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行任务
互斥锁,就是使用了线程同步技术
四:原子和非原子属性
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
atomic加锁原理
1 @property (assign, atomic) int age; 2 3 - (void)setAge:(int)age 4 { 5 6 @synchronized(self) { 7 _age = age; 8 } 9 }
原子和非原子属性的选择
nonatomic和atomic对比
atomic:线程安全,需要消耗大量的资源
nonatomic:非线程安全,适合内存小的移动设备
iOS开发的建议
所有属性都声明为nonatomic
尽量避免多线程抢夺同一块资源
尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力
转自:http://www.cnblogs.com/wendingding/tag/多线程篇/
相关文章推荐
- ios开发--xcode如何实现多工程联编
- iOS开发,更改状态栏(StatusBar)文字颜色为白色
- iOS实用代码片段
- iOS-动态创建按钮
- 27个提升效率的iOS开源库推荐
- IOS应用之间的跳转和数据传
- ios 图片处理( 1.按比例缩放 2.指定宽度按比例缩放
- iOS实现弹幕功能
- iOS自动处理键盘事件的第三方库:IQKeyboardManager
- iOS用AVSpeechSynthesizer合成语音
- iOS开发之如何获取各种控件的输入值
- iOS KVO概述及用法
- iOS 移动开发月报-第1期
- IOS中根据生日精确计算年龄
- 为学IOS,进击中...之OC多态练习
- OAuth认证 / 与新特性页面的配合使用
- iOS视图生命周期
- iOS应用程序生命周期
- IOS修改webView背景透明以及IOS调用前台js的方法
- 定期iOS的后台位置更新-- 相关资料收集,待有时间进行整理