執行個體解析iOS應用多線程開發中NSthread類的用法_IOS

來源:互聯網
上載者:User

一、NSthread的初始化
1.動態方法

複製代碼 代碼如下:

- (id)initWithTarget:(id)target selector:(SEL)selector object:(id)argument; 
// 初始化線程 
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.靜態方法

複製代碼 代碼如下:

+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument; 
[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.缺點:需要自己管理線程的生命週期,線程同步。線程同步對資料的加鎖會有一定的系統開銷

七、下載圖片的例子:
建立singeView app
建立項目,並在xib檔案上放置一個imageView控制項。按住control鍵拖到viewControll
er.h檔案中建立imageView IBOutlet
ViewController.m中實現:

複製代碼 代碼如下:

// 
//  ViewController.m 
//  NSThreadDemo 
// 
//  Created by rongfzh on 12-9-23. 
//  Copyright (c) 2012年 rongfzh. All rights reserved. 
// 
 
#import "ViewController.h" 
#define kURL @"http://avatar.csdn.net/2/C/D/1_totogo2010.jpg" 
@interface ViewController () 
 
@end 

複製代碼 代碼如下:
 
@implementation ViewController 
 
-(void)downloadImage:(NSString *) url{ 
    NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]]; 
    UIImage *image = [[UIImage alloc]initWithData:data]; 
    if(image == nil){ 
         
    }else{ 
        [self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES]; 
    } 

 
-(void)updateUI:(UIImage*) image{ 
    self.imageView.image = image; 

 
 
- (void)viewDidLoad 

    [super viewDidLoad]; 
     
//    [NSThread detachNewThreadSelector:@selector(downloadImage:) toTarget:self withObject:kURL]; 
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(downloadImage:) object:kURL]; 
    [thread start]; 

 
- (void)didReceiveMemoryWarning 

    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 

 
@end 

線程間通訊
線程下載完圖片後怎麼通知主線程更新介面呢?
複製代碼 代碼如下:

[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];

performSelectorOnMainThread是NSObject的方法,除了可以更新主線程的資料外,還可以更新其他線程的比如:
用:
複製代碼 代碼如下:
performSelector:onThread:withObject:waitUntilDone:

運行下載圖片:

圖片下載下來了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.