浅析iOS应用开发中线程间的通信与线程安全问题

2016-02-19 10:39 2 1 收藏

今天图老师小编给大家展示的是浅析iOS应用开发中线程间的通信与线程安全问题,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

线程间的通信
 
简单说明
线程间通信:在1个进程中,线程往往不是孤立存在的,多个线程之间需要经常进行通信
 
线程间通信的体现
1个线程传递数据给另1个线程
在1个线程中执行完特定任务后,转到另1个线程继续执行任务
 
线程间通信常用方法
代码如下:

- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait;
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait;

线程间通信示例 – 图片下载

代码如下:

//
//  YYViewController.m
//  06-NSThread04-线程间通信
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

#import "YYViewController.h"
@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end

代码如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

// 在子线程中调用download方法下载图片
    [self performSelectorInBackground:@selector(download) withObject:nil];
}

-(void)download
{
    //1.根据URL下载图片
    //从网络中下载图片
    NSURL *urlstr=[NSURL URLWithString:@"fdsf"];

    //把图片转换为二进制的数据
    NSData *data=[NSData dataWithContentsOfURL:urlstr];//这一行操作会比较耗时

    //把数据转换成图片
    UIImage *image=[UIImage imageWithData:data];
 
    //2.回到主线程中设置图片
    [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];
}

//设置显示图片
-(void)settingImage:(UIImage *)image
{
    self.iconView.image=image;
}

@end

代码2:
代码如下:

//
//  YYViewController.m
//  06-NSThread04-线程间通信
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"
#import NSData.h

@interface YYViewController ()
@property (weak, nonatomic) IBOutlet UIImageView *iconView;
@end

代码如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

 
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
// 在子线程中调用download方法下载图片

    [self performSelectorInBackground:@selector(download) withObject:nil];
}

 
-(void)download
{

    //1.根据URL下载图片
    //从网络中下载图片
    NSURL *urlstr=[NSURL URLWithString:@"fdsf"];

    //把图片转换为二进制的数据
    NSData *data=[NSData dataWithContentsOfURL:urlstr];//这一行操作会比较耗时

    //把数据转换成图片
    UIImage *image=[UIImage imageWithData:data];

    //2.回到主线程中设置图片
    //第一种方式
//    [self performSelectorOnMainThread:@selector(settingImage:) withObject:image waitUntilDone:NO];

    //第二种方式
    //    [self.imageView performSelector:@selector(setImage:) onThread:[NSThread mainThread] withObject:image waitUntilDone:NO];

    //第三种方式
   [self.iconView performSelectorOnMainThread:@selector(setImage:) withObject:image waitUntilDone:NO];
}

//设置显示图片
//-(void)settingImage:(UIImage *)image
//{
//    self.iconView.image=image;
//}

@end

线程安全
 
一、多线程的安全隐患
资源共享
1块资源可能会被多个线程共享,也就是多个线程可能会访问同一块资源
比如多个线程访问同一个对象、同一个变量、同一个文件
当多个线程访问同一块资源时,很容易引发数据错乱和数据安全问题
示例一:

示例二:

问题代码:
代码如下:

//
//  YYViewController.m
//  05-线程安全
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()
//剩余票数

@property(nonatomic,assign) int leftTicketsCount;
@property(nonatomic,strong)NSThread *thread1;
@property(nonatomic,strong)NSThread *thread2;
@property(nonatomic,strong)NSThread *thread3;

@end

代码如下:

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    //默认有20张票

    self.leftTicketsCount=10;

    //开启多个线程,模拟售票员售票

    self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];

    self.thread1.name=@"售票员A";

    self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];

    self.thread2.name=@"售票员B";

    self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];
    self.thread3.name=@"售票员C";
}

 
-(void)sellTickets
{
    while (1) {
        //1.先检查票数
        int count=self.leftTicketsCount;
        if (count0) {
            //暂停一段时间
            [NSThread sleepForTimeInterval:0.002];

            //2.票数-1
           self.leftTicketsCount= count-1;
 
            //获取当前线程
            NSThread *current=[NSThread currentThread];
            NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount);
        }else
        {
            //退出线程
            [NSThread exit];
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //开启线程

   [self.thread1 start];
    [self.thread2 start];
    [self.thread3 start];

}

@end

打印结果:

二、安全隐患分析

三、如何解决
 
互斥锁使用格式
@synchronized(锁对象) { // 需要锁定的代码  }
注意:锁定1份代码只用1把锁,用多把锁是无效的
 
代码示例:
代码如下:

//
//  YYViewController.m
//  05-线程安全
//
//  Created by apple on 14-6-23.
//  Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYViewController.h"

@interface YYViewController ()

//剩余票数
@property(nonatomic,assign) int leftTicketsCount;
@property(nonatomic,strong)NSThread *thread1;
@property(nonatomic,strong)NSThread *thread2;
@property(nonatomic,strong)NSThread *thread3;
@end

@implementation YYViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //默认有20张票
    self.leftTicketsCount=10;
    //开启多个线程,模拟售票员售票

    self.thread1=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];

    self.thread1.name=@"售票员A";

    self.thread2=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];

    self.thread2.name=@"售票员B";

    self.thread3=[[NSThread alloc]initWithTarget:self selector:@selector(sellTickets) object:nil];

    self.thread3.name=@"售票员C";
}

-(void)sellTickets
{
    while (1) {
        @synchronized(self){//只能加一把锁
        //1.先检查票数

        int count=self.leftTicketsCount;
        if (count0) {
            //暂停一段时间
            [NSThread sleepForTimeInterval:0.002];
            //2.票数-1

           self.leftTicketsCount= count-1;
            //获取当前线程
            NSThread *current=[NSThread currentThread];
            NSLog(@"%@--卖了一张票,还剩余%d张票",current,self.leftTicketsCount);

        }else
        {
            //退出线程
            [NSThread exit];
        }
        }
    }
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

    //开启线程
   [self.thread1 start];
    [self.thread2 start];
    [self.thread3 start];
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

@end

执行效果图

互斥锁的优缺点
优点:能有效防止因多线程抢夺资源造成的数据安全问题
缺点:需要消耗大量的CPU资源
 
互斥锁的使用前提:多条线程抢夺同一块资源
相关专业术语:线程同步,多条线程按顺序地执行任务
互斥锁,就是使用了线程同步技术
 
四:原子和非原子属性
 
OC在定义属性时有nonatomic和atomic两种选择
atomic:原子属性,为setter方法加锁(默认就是atomic)
nonatomic:非原子属性,不会为setter方法加锁
 
atomic加锁原理
代码如下:

@property (assign, atomic) int age;

- (void)setAge:(int)age
{

    @synchronized(self) {
       _age = age;
    }
}

原子和非原子属性的选择
nonatomic和atomic对比

atomic:线程安全,需要消耗大量的资源 nonatomic:非线程安全,适合内存小的移动设备 

iOS开发的建议

所有属性都声明为nonatomic 尽量避免多线程抢夺同一块资源 尽量将加锁、资源抢夺的业务逻辑交给服务器端处理,减小移动客户端的压力

来源:https://www.tulaoshi.com/n/20160219/1595346.html

延伸阅读
当前流行的Windows操作系统能同时运行几个程序(独立运行的程序又称之为进程),对于同一个程序,它又可以分成若干个独立的执行流,我们称之为线程,线程提供了多任务处理的能力。用进程和线程的观点来研究软件是当今普遍采用的方法,进程和线程的概念的出现,对提高软件的并行性有着重要的意义。现在的大型应用软件无一不是多线程多任务处理...
一、实现一个简单的tableView显示效果 实现效果展示: 代码示例(使用以前在主控制器中进行业务处理的方式) 1.新建一个项目,让控制器继承自UITableViewController。 代码如下: // //  YYViewController.h //  01-自定义Operation // //  Created by apple on 14-6-26. //  Copyright (c) 2014年 itcase. All ...
标签: PHP
PHP中实现多线程? 看到这个标题, 你一定以为我疯了..但是事实上我真的这么做了. 下面是我的一些做法, 已经实验过. 确实可以的. 我们知道PHP本身是不支持多线程的, 但是我们的WEB服务器是支持多线程的. 也就是说可以同时让多人一起访问. 这也是我在PHP中实现多线程的基础. 假设我们现在运行的是a.php这个文件. 但是我在程序中...
标签: Java JAVA基础
  选择自 hf1650 的 Blog 一、概述 当网络编程越来越方便,系统功能越来越强大,安全性却指数倍地下降。这恐怕就是网络编程的不幸和悲哀了。各种动态内容生成环境繁荣了WWW,它们的设计目标就是为了给开发者更多的力量,给最终用户更多的方便。正因为如此,系统设计师和开发者必须明确地把安全问题作为一个考虑因素,事后追悔很...

经验教程

838

收藏

81
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部