国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > iOS > 正文

實例解析iOS應用多線程開發中NSthread類的用法

2019-10-21 18:56:00
字體:
來源:轉載
供稿:網友
這篇文章主要介紹了iOS應用多線程開發中NSthread類的用法,代碼基于傳統的Objective-C,NSthread類需要的朋友可以參考下
 

一、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:

運行下載圖片:

 

實例解析iOS應用多線程開發中NSthread類的用法

圖片下載下來了。



注:相關教程知識閱讀請移步到IOS開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长海县| 祥云县| 辛集市| 青河县| 达日县| 枣阳市| 陆丰市| 聂荣县| 白银市| 舒兰市| 尉氏县| 陈巴尔虎旗| 阿坝县| 卢氏县| 乳源| 铜陵市| 通榆县| 门源| 名山县| 额尔古纳市| 玉环县| 蒙阴县| 台湾省| 罗城| 灵石县| 普陀区| 五常市| 增城市| 海门市| 延吉市| 西乡县| 武川县| 玛纳斯县| 观塘区| 安吉县| 和硕县| 文昌市| 锡林郭勒盟| 涿鹿县| 武乡县| 蓬安县|