iOS开发多线程篇—线程安全 - 文顶顶

原文  http://www.cnblogs.com/wendingding/p/3805841.html

iOS开发多线程篇—线程安全

一、多线程的安全隐患

资源共享

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
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     [email protected]"售票员A";
40
41     self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
42
43     [email protected]"售票员B";
44
45     self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
46     [email protected]"售票员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

打印结果:

二、安全隐患分析

三、如何解决

互斥锁使用格式

@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     [email protected]"售票员A";
32
33     self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
34
35     [email protected]"售票员B";
36
37     self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
38
39     [email protected]"售票员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

执行效果图

互斥锁的优缺点

优点:能有效防止因多线程抢夺资源造成的数据安全问题

缺点:需要消耗大量的 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

尽量避免多线程抢夺同一块资源

尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力

时间: 2024-07-30 13:43:13

iOS开发多线程篇—线程安全 - 文顶顶的相关文章

iOS开发多线程篇—GCD介绍 - 文顶顶

原文  http://www.cnblogs.com/wendingding/p/3806821.html iOS开发多线程篇-GCD介绍 一.简单介绍 1.什么是GCD? 全称是Grand Central Dispatch,可译为“牛逼的中枢调度器” 纯C语言,提供了非常多强大的函数 2.GCD的优势 GCD是苹果公司为多核的并行运算提出的解决方案 GCD会自动利用更多的CPU内核(比如双核.四核) GCD会自动管理线程的生命周期(创建线程.调度任务.销毁线程) 程序员只需要告诉GCD想要执行

iOS开发多线程篇—NSOperation基本操作 - 文顶顶

原文  http://www.cnblogs.com/wendingding/p/3809150.html iOS开发多线程篇—NSOperation基本操作 一.并发数 (1)并发数:同时执?行的任务数.比如,同时开3个线程执行3个任务,并发数就是3 (2)最大并发数:同一时间最多只能执行的任务的个数. (3)最?大并发数的相关?方法 - (NSInteger)maxConcurrentOperationCount; - (void)setMaxConcurrentOperationCount

iOS开发多线程篇—线程的状态 - 文顶顶

原文  http://www.cnblogs.com/wendingding/p/3807184.html iOS开发多线程篇-线程的状态 一.简单介绍 线程的 创建 : self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 说明:创建线程有多种方式,这里不做过多的介绍. 线程的 开启 : [self.thread start]; 线程的 运行 和 阻塞 : (1)设置线程阻塞

iOS开发多线程篇—线程的状态

iOS开发多线程篇—线程的状态 一.简单介绍 线程的创建: self.thread=[[NSThread alloc]initWithTarget:self selector:@selector(test) object:nil]; 说明:创建线程有多种方式,这里不做过多的介绍. 线程的开启: [self.thread start]; 线程的运行和阻塞: (1)设置线程阻塞1,阻塞2秒 [NSThread sleepForTimeInterval:2.0]; (2)第二种设置线程阻塞2,以当前时

iOS开发多线程篇—线程间的通信

iOS开发多线程篇—线程间的通信 一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 线程间通信常用方法 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; - (void)performSelector:(SE

iOS开发多线程篇—线程安全

iOS开发多线程篇—线程安全 一.多线程的安全隐患 资源共享 1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源 比如多个线程访问同一个对象.同一个变量.同一个文件 当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题 示例一: 示例二: 问题代码: 1 // 2 // YYViewController.m 3 // 05-线程安全 4 // 5 // Created by apple on 14-6-23. 6 // Copyright (c) 2014年 itcase.

iOS开发多线程篇—线程间的通信(转)

这里转载 给自己一个备份 一.简单说明 线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信 线程间通信的体现 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 线程间通信常用方法 - (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait; - (void)performSelector:(SEL)aS

iOS开发多线程篇---线程锁(线程安全)

@interface BTThreadViewController () {     NSThread *OneThread;//师傅一     NSThread *TwoThread;//师傅二     NSThread *ThreeThread;//师傅三     int allCake;//蛋糕总数 } @end @implementation BTThreadViewController - (id)initWithNibName:(NSString *)nibNameOrNil bun

iOS开发多线程篇---线程间的通信

线程间通信的表现: 1个线程传递数据给另1个线程 在1个线程中执行完特定任务后,转到另1个线程继续执行任务 下面是一个下载图片的例子: @interface BTThreadViewController () {     UIImageView *imagev; } @end - (void)viewDidLoad {     [super viewDidLoad];          imagev = [[UIImageView alloc] initWithFrame:CGRectMake(