淺析iOS應用開發中線程間的通訊與安全執行緒問題_IOS

來源:互聯網
上載者:User

線程間的通訊
 
簡單說明
線程間通訊:在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.
//

#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 (count>0) {
            //暫停一段時間
            [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 (count>0) {
            //暫停一段時間
            [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


執行效果圖

互斥鎖的優缺點
優點:能有效防止因多線程搶奪資源造成的資料安全問題
缺點:需要消耗大量的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
  • 盡量避免多線程搶奪同一塊資源
  • 盡量將加鎖、資源搶奪的商務邏輯交給伺服器端處理,減小移動用戶端的壓力
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.