iOS 多線程技術1

來源:互聯網
上載者:User

標籤:顯示   nsthread   執行個體   ios   

iOS 多線程技術1

iOS 有三種多線程編程技術:

  • NSThread
  • NSOperation
  • GCD

它們的抽象程度由低到高,越高的使用起來越簡單.

NSThread顯示調用 NSthread 類
  • 類方法
+ (void)detachNewThreadSelector:(SEL)selector toTarget:(id)target withObject:(id)argument;
  • 執行個體方法
- (instancetype)initWithTarget:(id)target selector:(SEL)selector object:(id)argument [thread start]
隱式調用
  • 開啟後台線程
- (void)performSelectorInBackground:(SEL)aSelector withObject:(id)arg
  • 在主線程中運行
- (void)performSelectorOnMainThread:(SEL)aSelector withObject:(id)arg waitUntilDone:(BOOL)wait
  • 在指定的線程中執行,但是該線程必須具備 run loop
- (void)performSelector:(SEL)aSelector onThread:(NSThread *)thr withObject:(id)arg waitUntilDone:(BOOL)wait
常見NSThread 的方法
+ (NSThread *)currentThread; //獲得當前線程+ (void)sleepForTimeInterval:(NSTimeInterval)ti; //線程休眠+ (NSThread *)mainThread; //主線程,即 UI線程+ (BOOL)isMainThread; //判斷是否主線程- (BOOL)isMainThread; //同上- (BOOL)isExecuting; // 線程是否正在運行- (BOOL)isFinished; // 線程是否已結束

Demo:
建立一個 lable, 實現程式運行起來一段時間後就能顯示一些內容,並且有一個 button 的按鈕,當點擊時,類比從網路上重新整理出一張圖片,從中我們要瞭解到當需要更新 UI 時,必須在主線程中進行.從使用者的角度來說,我看一張圖片,不能影響的我在介面的其他動作,否則,這個 app 用著體驗太差,我可能會刪掉不再使用.

@interface ViewController ()@property (weak, nonatomic) IBOutlet UIImageView *imageView;@property (weak, nonatomic) IBOutlet UILabel *lable;@end@implementation ViewController- (void)viewDidLoad {    [super viewDidLoad];    // 1.類方法    [NSThread detachNewThreadSelector:@selector(dosomething:) toTarget:self withObject:@"detachNewThreadSelector"];}- (void)dosomething:(NSString * )string{    // 休眠5秒鐘,只是類比一個耗時的操作,如果操作耗時,就另起一個線程執行    [NSThread sleepForTimeInterval:5];    //更新 UI, 必須在主線程中進行  //NO 非同步  YES 同步    [self performSelectorOnMainThread:@selector(updateLable:) withObject:string waitUntilDone:NO];    NSLog(@"%s",__func__);}- (void)updateLable:(NSString*)string{    _lable.text = string;}#pragma mark - image view- (IBAction)btnAction:(UIButton *)sender {    //執行個體方法    NSString *urlStr =  @"http://img.hb.aicdn.com/5a8f57157b47284724d09ffd2da28369731f8144ac9c-1XdZKJ_fw658";    NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(fetchImage:) object:urlStr];    thread.name = @"lisi";    [thread start];}- (void)fetchImage:(NSString*)urlStr{    // 進行耗時的網路請求    [NSThread sleepForTimeInterval:4];    NSURL *url = [NSURL URLWithString:urlStr];    NSData *data = [NSData dataWithContentsOfURL:url];    UIImage *image = [UIImage imageWithData:data];    if ([[NSThread currentThread].name isEqualToString:@"lisi"]) {        BOOL isMainThread = [[NSThread currentThread] isMainThread];        NSLog(@">>>>>isMainThread:%d",isMainThread);    }else{        BOOL isMainThread = [[NSThread currentThread] isMainThread];        NSLog(@"<<<<<<isMainThread:%d",isMainThread);    }    [self performSelectorOnMainThread:@selector(updateImageView:) withObject:image waitUntilDone:YES];}- (void)updateImageView:(UIImage *)image{    if ([[NSThread currentThread] isMainThread]) {        NSLog(@"主線程:%s",__func__);    }    _imageView.image = image;}

運行結果:
2015-08-02 14:49:31.823 03-NSThreadDemo[3151:781147] -[ViewController dosomething:]
2015-08-02 14:49:34.539 03-NSThreadDemo[3151:781778] >>>>>isMainThread:0
2015-08-02 14:49:34.539 03-NSThreadDemo[3151:780917] 主線程:-[ViewController updateImageView:]

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

iOS 多線程技術1

聯繫我們

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