你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> 實例解析iOS運用多線程開辟中NSthread類的用法

實例解析iOS運用多線程開辟中NSthread類的用法

編輯:IOS開發綜合

1、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]; 

2、獲得以後線程

NSThread *current = [NSThread currentThread];
 

3、獲得主線程

NSThread *main = [NSThread mainThread];

4、暫就緒前哨程

// 暫停2s 
[NSThread sleepForTimeInterval:2];   
// 或許 
NSDate *date = [NSDate dateWithTimeInterval:2 sinceDate:[NSDate date]]; 
[NSThread sleepUntilDate:date]; 

5、線程間的通訊
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]; 

6、優缺陷
1.長處:NSThread比其他兩種多線程計劃較輕量級,更直不雅地掌握線程對象
2.缺陷:須要本身治理線程的性命周期,線程同步。線程同步對數據的加鎖會有必定的體系開支

7、下載圖片的例子:
新建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:
運轉下載圖片:

201621591510906.png (320×590)

圖片下載上去了。

【實例解析iOS運用多線程開辟中NSthread類的用法】的相關資料介紹到這裡,希望對您有所幫助! 提示:不會對讀者因本文所帶來的任何損失負責。如果您支持就請把本站添加至收藏夾哦!

  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved